|
-
May 5th, 2002, 05:44 PM
#1
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
Last edited by MrPolite; May 8th, 2002 at 12:02 PM.
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
May 7th, 2002, 12:07 PM
#2
bump bump bump
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
May 7th, 2002, 12:12 PM
#3
it may not be exiting that loop so the file doesnt get closed.
i dont think
If strLine = Nothing Then
is proper
-
May 8th, 2002, 11:53 AM
#4
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
May 8th, 2002, 03:10 PM
#5
How bout this
Do While str.Length > 0
strLine = sr.ReadLine()
Loop
sr.close
let me know if it works for ya
-
May 8th, 2002, 06:47 PM
#6
-
Nov 3rd, 2006, 05:15 PM
#7
New Member
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!
-
Nov 3rd, 2006, 10:06 PM
#8
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
-
Nov 5th, 2006, 12:36 PM
#9
New Member
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!
Last edited by gazzat5; Nov 5th, 2006 at 12:39 PM.
-
Nov 5th, 2006, 06:50 PM
#10
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|