
May 4th, 2008, 12:08 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 11
Time spent in forums: 3 h 19 m 22 sec
Reputation Power: 0
|
|
|
Help with reading file to buffer & sending
Hi,
I want to read the text file and put each line into a buffer before i send it to the 2nd listbox. Can anyone check out the code and point me in the right direction.
Code:
Public Class Form1
Dim line As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Reads contents of c:\encrypt.txt
Dim fileContents() As String = System.IO.File.ReadAllLines("C:\message.txt")
For i As Integer = 0 To fileContents.GetUpperBound(0)
fileContents(i) = System.IO.Path.GetFileNameWithoutExtension(fileContents(i))
' ListBox1.Items.Add(fileContents(i))
If i Mod 2 = 0 Then
ListBox1.Items.Add(fileContents(i))
Else
ListBox2.Items.Add(fileContents(i))
End If
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
ProgressBar1.Maximum = ListBox1.Items.Count - 1
ProgressBar1.Step = 1
ListBox1.Items.Clear()
Dim myfile = ListBox1.Items(i)
Dim mystream As New IO.FileStream(myfile, IO.FileMode.Open, IO.FileAccess.Read) ' Create a new file stream to hold the contents of the file
Dim sr As New IO.StreamReader(mystream) ' Use SR to read our new stream
Dim buffer As String = Nothing ' Used as a temporary holder
Do While sr.EndOfStream = False ' Do the loop until we have reached the end of the file
buffer = sr.ReadLine ' Throw the line into buffer
If buffer <> Nothing And buffer.StartsWith("#") Then ' If buffer is not a blank line or does not start with @
ListBox2.Items.Add(buffer + " " + (System.Text.RegularExpressions.Regex.Match(buffer, "(\w+)").Groups(1).ToString()))
Else
ListBox2.Items.Add(buffer)
End If
Loop
sr.Close() ' Close the stream reader.
mystream.Close() ' Close the stream.
Dim i1 As Integer
Dim s As String
If ListBox1.Items.Count > 0 Then
Dim file As New System.IO.StreamWriter(ListBox1.Items.Item(i).ToString + "c:\sendmessage.txt", True, System.Text.Encoding.UTF8)
For i1 = 0 To ListBox2.Items.Count - 1
s = ListBox2.Items.Item(i1)
file.WriteLine(s)
Next i1
file.Close()
End If
ProgressBar1.PerformStep()
Next
End Sub
End Class
|