Why does my file get locked?!
I am simply reading from a textfile, but it gets locked (cant write on it), until I close my program:
VB Code:
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:(
Re: Why does my file get locked?!
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:
VB Code:
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!
Re: Why does my file get locked?!
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:
VB Code:
Using fileObj As New FileStream(...)
'Use fileObj here.
End Using
The 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:
VB Code:
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
Re: Why does my file get locked?!
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:
VB 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!
Re: Why does my file get locked?!
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.