Results 1 to 10 of 10

Thread: Why does my file get locked?!

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Question 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:
    1. Dim sr As StreamReader = File.OpenText(strFullPath)
    2.             Dim strLine As String
    3.  
    4.             Do
    5.                 strLine = sr.ReadLine()
    6.                 If strLine = Nothing Then
    7.                     Exit Do
    8.                 End If
    9.  
    10.                 ' Some other code here ... :P
    11.             Loop
    12.             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!!

  2. #2

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    it may not be exiting that loop so the file doesnt get closed.

    i dont think
    If strLine = Nothing Then

    is proper

    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Well the problem is that I dunno the proper way. 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
    I cant figure out why it locks the file
    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!!

  5. #5
    egiggey
    Guest
    How bout this

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

    sr.close

    let me know if it works for ya

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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
    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
    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!!

  7. #7
    New Member
    Join Date
    Jun 2006
    Posts
    4

    Question 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:
    1. Try
    2.                 Dim fileObj As New System.IO.FileStream(Path & "ImageInfo.xml", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)
    3.                 Dim attr As FileAttributes = File.GetAttributes(Path & "ImageInfo.xml")
    4.                 Nonwritable = False
    5.                 If (attr And FileAttributes.ReadOnly) <> 0 Then
    6.                     NonWritable = True
    7.                 End If
    8.             Catch ex As exception
    9.                 nonwritable = True
    10.             End Try

    Any suggestions ideas etc much appreciated!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Using fileObj As New FileStream(...)
    2.     'Use fileObj here.
    3. 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:
    1. Dim fileObj As FileStream
    2.  
    3. Try
    4.     fileObj = New FileStream(...)
    5.  
    6.     'Use fileObj here.
    7. Catch
    8.  
    9. Finally
    10.     If Not fileObj Is Nothing Then
    11.         fileObj.Dispose()
    12.     End If
    13. End Try
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    New Member
    Join Date
    Jun 2006
    Posts
    4

    Question 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:
    1. Try
    2.             Dim tmpguid As String = Guid.NewGuid.ToString       ' Generate random filename.
    3.             File.Create(path & "testfile" & tmpguid & ".tmp")   ' Create a testfile.
    4.             Try
    5.                 File.Delete(".\testfile" & tmpguid & ".tmp")
    6.             Catch ex As Exception
    7.                 ' Couldn't del file!
    8.                 MsgBox(ex.ToString)
    9.             End Try
    10.             ' Creating the file did not cause an exception; therefore:
    11.             NonWritable = False
    12.         Catch ex As Exception
    13.             MsgBox(ex)
    14.             NonWritable = True
    15.         End Try

    Many thanks!
    Last edited by gazzat5; Nov 5th, 2006 at 12:39 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width