Hi, I'm trying to write a program which, amongst other things, adds text to an existing text file. The way I'm attempting this is to write the new text file with a new name, and then delete the old one, and then rename the new file to the name of the previous one. Yeah I know, convoluted way of going about it, but just at the moment that's what I'm trying...
Ok, so I've down-loaded the existing file, and it does what it's supposed to do that's fine, but when I come to the part of the project which is supposed to add the new line of text, I've made the new file, and tested that it's there and has the new text added ( I used 'Process.Start(OutPath)', where 'OutPath' is the File Path and Name of the new file) by simply reading it.
Then comes the tricky bit... Here is the 'Read-in the original file' code:
When I come to delete the old file:
I get an error message telling me that windows can't delete ' "C:\Program Files (x86)\Visual Studio 2010\Projects\Txt Talk\Dictionary.txt" ' "Because it's in use by another process".
I thought 'objRead.Close()' (Line 25) was supposed to close that file, and as far as I can tell, it's not open anywhere else... There's a lot of code between the 'Close' and the 'Delete', there's the print the new file and open it at the very least.
Poppa.
Ok, so I've down-loaded the existing file, and it does what it's supposed to do that's fine, but when I come to the part of the project which is supposed to add the new line of text, I've made the new file, and tested that it's there and has the new text added ( I used 'Process.Start(OutPath)', where 'OutPath' is the File Path and Name of the new file) by simply reading it.
Then comes the tricky bit... Here is the 'Read-in the original file' code:
vb.net Code:
Dim FilePath, OutPath As String Private Sub Initialise() Dim a As Integer, z, y As String FilePath = "C:\Program Files (x86)\Visual Studio 2010\Projects\Txt Talk\Dictionary.txt" ComboBox1.Sorted = False ComboBox2.Sorted = False ComboBox1.Items.Clear() ComboBox2.Items.Clear() ComboBox1.Items.Add("") 'I want an empty item to start with. ComboBox2.Items.Add("") TextBox1.Text = "" Dim objRead As New System.IO.StreamReader(FilePath, System.Text.Encoding.Default) Do While objRead.Peek <> -1 z = objRead.ReadLine a = InStr(z, "=") y = z.Substring(a + 1).Trim z = z.Substring(0, a - 1).Trim ComboBox1.Items.Add(z) ComboBox2.Items.Add(y) Loop objRead.Close() Else MsgBox("Can't find file " & FilePath) End If End Sub
vb.net Code:
I get an error message telling me that windows can't delete ' "C:\Program Files (x86)\Visual Studio 2010\Projects\Txt Talk\Dictionary.txt" ' "Because it's in use by another process".
I thought 'objRead.Close()' (Line 25) was supposed to close that file, and as far as I can tell, it's not open anywhere else... There's a lot of code between the 'Close' and the 'Delete', there's the print the new file and open it at the very least.
Poppa.