PDA

Click to See Complete Forum and Search --> : Why does my file get locked?!


MrPolite
May 5th, 2002, 05:44 PM
I am simply reading from a textfile, but it gets locked (cant write on it), until I close my program:

Dim sr As StreamReader = File.OpenText(strFullPath)
Dim strLine As String

Do
strLine = sr.ReadLine()
If strLine = Nothing Then
Exit Do
End If

' Some other code here ... :P
Loop
sr.Close()


I'm closing the streamReader, but still the file is locked. I tried to do grabage collecting too, but that doesn't work.
Someone help plz:(

MrPolite
May 7th, 2002, 12:07 PM
bump bump bump:p

Cander
May 7th, 2002, 12:12 PM
it may not be exiting that loop so the file doesnt get closed.

i dont think
If strLine = Nothing Then

is proper

:confused:

MrPolite
May 8th, 2002, 11:53 AM
Well the problem is that I dunno the proper way:D. How do you check for the end of file in VB.NET?

Originally posted by Cander
it may not be exiting that loop so the file doesnt get closed.

umm, it shouldn't be because of exiting the Do...Loop anyways, because I'm closing the StreamReader at the end.
I don't know, I dont have a book yet and I haven't read much about files and stuff in VB.NET. Someone plz tell me if I should even use the close method:p
I cant figure out why it locks the file:confused:

egiggey
May 8th, 2002, 03:10 PM
How bout this

Do While str.Length > 0
strLine = sr.ReadLine()
Loop

sr.close

let me know if it works for ya

MrPolite
May 8th, 2002, 06:47 PM
AAA!!!! I'm dumb!!!! I was returning a value IN THE DO LOOP so it was exiting the function before reaching sr.close()!!!!!!!!!!!!!!
Sorry everyone if I wasted your time on this, it was kinda stupid :p
but I found one single thing at least: I would use this way rather than strLine = Nothing

Do

Loop While Sr.Peek <> -1


that peek thingie does the work:p

gazzat5
Nov 3rd, 2006, 04:15 PM
I have another question about file locking that i will post here unless someone suggests i should start a new topic...

The problem is i have a XML database file which is getting locked. It is read into a dataset using the readxml method. It is also written using a SteamWriter - Yes in know i should probably use the xml writer, but it works! - and but since that is closed, i assume that is probably not the problem.

I was wondering if attr FileAttribute variable in the following code, used to check whether the DB can be written to is the problem:
Try
Dim fileObj As New System.IO.FileStream(Path & "ImageInfo.xml", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)
Dim attr As FileAttributes = File.GetAttributes(Path & "ImageInfo.xml")
Nonwritable = False
If (attr And FileAttributes.ReadOnly) <> 0 Then
NonWritable = True
End If
Catch ex As exception
nonwritable = True
End Try

Any suggestions ideas etc much appreciated!

jmcilhinney
Nov 3rd, 2006, 09:06 PM
The issue is exactly the same as the old thread that you dredged up. You're opening a FileStream, which locks the file, and then never closing it. If you're using VB 2005 then you should make use of the 'Using' keyword:Using fileObj As New FileStream(...)
'Use fileObj here.
End UsingThe object created on the first line is always Disposed when the block is exited. It doesn't matter how the block is exited, the object is still Disposed, so the file will always be closed and the lock released. This is good practice for more than just this reason too. Any objects with a Dispose method should be instantiated with a Using block if they are only used in a brief section of code like this. If you're not using VB 2005 then the equivalent of the code above is:Dim fileObj As FileStream

Try
fileObj = New FileStream(...)

'Use fileObj here.
Catch

Finally
If Not fileObj Is Nothing Then
fileObj.Dispose()
End If
End Try

gazzat5
Nov 5th, 2006, 11:36 AM
Ok many thanks for your help on that. I will make sure all streams are disposed of safely in future!

I have yet another file locking issue in the same procedure. Here i create a temporary file and delete it. If there is another way of generating a temporary file let me know.

Heres the offending code:

Try
Dim tmpguid As String = Guid.NewGuid.ToString ' Generate random filename.
File.Create(path & "testfile" & tmpguid & ".tmp") ' Create a testfile.
Try
File.Delete(".\testfile" & tmpguid & ".tmp")
Catch ex As Exception
' Couldn't del file!
MsgBox(ex.ToString)
End Try
' Creating the file did not cause an exception; therefore:
NonWritable = False
Catch ex As Exception
MsgBox(ex)
NonWritable = True
End Try

Many thanks!

jmcilhinney
Nov 5th, 2006, 05:50 PM
Please don't hijack existing threads, especially for unrelated questions. One thread per topic and one topic per thread.

Use the IO.Path.GetTempFileName method.