6

I'm using IO.Directory.GetFiles to search for files in a folder. After the searching is done, I can't use files in this folder until my application is closed. I haven't found any Dispose functions in DirectoryInfo class, so my question is: How can I release or unlock these files?

My code:

Dim list = IO.Directory.GetFiles(folder, "*.*", IO.SearchOption.AllDirectories)

EDIT:

I have examined my code once again very carefully (I couldn't reproduce my problem in another project) and it turned out that this function is locking the files:

   Public Function ComputeFileHash(ByVal filePath As String)
        Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
        Dim f As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        f = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        md5.ComputeHash(f)
        f.Close()
        f.Dispose()
        Dim hash As Byte() = md5.Hash
        Dim buff As Text.StringBuilder = New Text.StringBuilder
        Dim hashByte As Byte
        For Each hashByte In hash
            buff.Append(String.Format("{0:X2}", hashByte))
        Next
        Dim md5string As String
        md5string = buff.ToString()
        Return md5string
    End Function

It's strange. I'm closing the FileStream and disposing the whole object but file remains locked.

user3561694
  • 105
  • 1
  • 9
  • 1
    +1. You should not be getting any locks. Read never causes locks, only write does. Can you try to come up with a reduced test case, which you can post here? Like 5 files in 3 files causing the same issue. – Victor Zakharov May 26 '14 at 20:41
  • 1
    What are you doing with the files after you have the names? I can't reproduce the problem - i used the code that you posted to fetch the names, then i read the content of couple of them and all worked well. – keenthinker May 26 '14 at 20:52
  • I've updated my question with relevant information. – user3561694 May 26 '14 at 20:58
  • Are you using Antivirus software? If so, try disabling it as it [sometimes keeps handles open](http://stackoverflow.com/questions/6350224/does-filestream-dispose-close-the-file-immediately) for a while after you've accessed it. – Basic May 26 '14 at 21:07

1 Answers1

9

You are opening 2 seperate streams, then only closing the last one.

 Dim f As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
 f = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)

The first line creates a new filestream instance then, before it can be used, the 2nd line creates a NEW instance and throws out the original one without disposing it.

You should only need one of these lines.

I recommend:

Dim f As New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • Thanks. It works like a charm now. I'd press that upvote button so hard if I'd have enough reputation! – user3561694 May 26 '14 at 21:17
  • `FileSteam.Close()` http://msdn.microsoft.com/en-us/library/aa328800%28v=vs.71%29.aspx or `Using` statement: http://msdn.microsoft.com/en-us/library/htd05whh.aspx, `iDisposable`: http://msdn.microsoft.com/en-ca/library/system.idisposable.aspx eh, you are using them. then there's something else wrong :\ oh right... you don't need to call both `Close()` and `Dispose()`. one is enough. – porkchop May 27 '14 at 22:21