Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27446

File doesn't close ?

$
0
0
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:
vb.net Code:
  1. Dim FilePath, OutPath As String
  2.  
  3. Private Sub Initialise()
  4.       Dim a As Integer, z, y As String
  5.  
  6.       FilePath = "C:\Program Files (x86)\Visual Studio 2010\Projects\Txt Talk\Dictionary.txt"
  7.       ComboBox1.Sorted = False
  8.       ComboBox2.Sorted = False
  9.       ComboBox1.Items.Clear()
  10.       ComboBox2.Items.Clear()
  11.       ComboBox1.Items.Add("")     'I want an empty item to start with.
  12.       ComboBox2.Items.Add("")
  13.       TextBox1.Text = ""
  14.  
  15.       If System.IO.File.Exists(FilePath) Then
  16.          Dim objRead As New System.IO.StreamReader(FilePath, System.Text.Encoding.Default)
  17.          Do While objRead.Peek <> -1
  18.             z = objRead.ReadLine
  19.             a = InStr(z, "=")
  20.             y = z.Substring(a + 1).Trim
  21.             z = z.Substring(0, a - 1).Trim
  22.             ComboBox1.Items.Add(z)
  23.             ComboBox2.Items.Add(y)
  24.          Loop
  25.          objRead.Close()
  26.       Else
  27.          MsgBox("Can't find file " & FilePath)
  28.       End If
  29.  
  30.    End Sub
When I come to delete the old file:
vb.net Code:
  1. ObjWrite.Close()
  2.          Process.Start(OutPath)
  3.          System.IO.File.Delete(FilePath)
  4.          System.IO.File.Copy(OutPath, FilePath, True)

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.

Viewing all articles
Browse latest Browse all 27446

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>