PDA

Click to See Complete Forum and Search --> : file in use error


Sneeden
Nov 16th, 2001, 04:42 PM
I am using this loop several times in my app
Dim X As Short
For X = 1 To 5
Dim FSO As File
Dim FStreamObject As FileStream
FSO.Create(Application.StartupPath & "\" & X & ".txt")
FSO.SetAttributes(Application.StartupPath & "\" & X & ".txt", FileAttributes.Archive)
Next

It works fine the first time I go throught the loop. The problem is when it goes through the loop a second time, it generates the error "The process cannot access the file "C:\1.txt" because it is being used by another process.

Is there a way to release the handle on the file?

Also, I found how to write a byte array to a text file, but how do I write a string type?



Thanks

zchoyt
Nov 18th, 2001, 09:14 PM
Dim fFile as File works fine the first time, but generates an error if you use it a second time. It has no Close method.


You can use this
'To create a file
Dim fFileStream as FileStream = new FileStream("c:\test.txt", FileMode.Create)

'to write to a file
Dim fStreamWriter as StreamWriter = New StreamWriter("c:\test.txt")
fStreamWriter.Write("I am writing to the file now!" & vbcrlf)
'this will release the applications "Grip" on the file
fStreamWriter.Close

'to read from a file
'dim fStreamReader as StreamReader = New StreamReader("c:\test.txt")
Messagebox.Show(fStreamReader.ReadLine())
'this will release the applications "Grip" on the file
fStreamReader.Close