1
 Dim FolderName As New IO.DirectoryInfo(FileLocationFolder)
 Dim diar1 As IO.FileInfo() = FolderName.GetFiles("*.xml")

I am looping through diar1 like the following

 For Each CurrFile In diar1
 Next 

Does that lock all the files in that directory?

If so, how do I dispose it after I am done? Any help would be appreciated.

Here is the rest of my code:

Public Sub Get_FTP_video(ByVal curr As Integer)
    Try

        Dim FolderName As New IO.DirectoryInfo(FileLocationFolder)
        Dim diar1 As IO.FileInfo() = FolderName.GetFiles("*.xml")

        Dim CurrFile As IO.FileInfo
        Dim CurrfileName As String
        Dim currsize, MinSize As Integer
        currsize = 0
        MinSize = 0

        For Each CurrFile In diar1
            CurrfileName = CurrFile.Name

            If CurrFile.CreationTime.Date = Today.Date Then
                GET_FileName(FileLocationFolder & CurrfileName, currsize)
                If MinSize = 0 Then
                    MinSize = currsize
                Else
                    If currsize < MinSize Then
                        splitter = CurrFile.Name.Split(".")
                        If My.Computer.FileSystem.FileExists(FileLocationFolder & splitter(0).ToString & ".mp4") Then
                            MinSize = currsize
                            newVideoName = splitter(0).ToString
                        End If
                    End If
                End If
            End If
        Next

        System.IO.File.Move(FileLocationFolder & videonamee & ".mp4", MoveFileToFolder & videonamee)
        sendmessage = True

        MoveAllFilesToBin()

     Catch ex As System.Data.SqlClient.SqlException
     Catch ex As Exception 
     End Try
End Sub

'

Public Sub GET_FileName(ByVal directorytracer As String, ByRef fileSize As Integer)
        Try
            Dim xmldoc As New XmlDataDocument()
            Dim mmm As New XmlDataDocument
            Dim xmlnode As XmlNodeList
            Dim i As Integer
            Dim str As String
            Dim fs As New FileStream(directorytracer, FileMode.Open, FileAccess.Read)
            xmldoc.Load(fs)
            Dim ds As New DataSet
            Dim xmlNodeRdr As XmlNodeReader
            xmlnode = xmldoc.GetElementsByTagName("item")

            For i = 0 To xmlnode.Count - 1
                str = xmlnode(i).ChildNodes.Item(3).InnerXml
                mmm.InnerXml = str
                xmlNodeRdr = New XmlNodeReader(mmm)
                ds.ReadXml(xmlNodeRdr)
                fileSize = CInt(ds.Tables(0).Rows(0).Item(0).ToString)
            Next

        Catch ex As System.Data.SqlClient.SqlException

        Catch ex As Exception

        End Try

    End Sub

.

The error occurs in this function:

Sub MoveAllFilesToBin()
    Try
        Dim FolderName As New IO.DirectoryInfo(FileLocationFolder)
        Dim diar1 As IO.FileInfo() = FolderName.GetFiles()
        For Each CurrFile In diar1
            If System.IO.File.Exists(BinFolderFTP & CurrFile.Name) Then
                System.IO.File.Delete(BinFolderFTP & CurrFile.Name)
            End If
            System.IO.File.Move(FileLocationFolder & CurrFile.Name, BinFolderFTP & CurrFile.Name)
        Next

        Catch ex As System.Data.SqlClient.SqlException
        Catch ex As Exception
    End Try
End Sub

.

The process cannot access the file because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.__Error.WinIOError()
   at System.IO.File.Move(String sourceFileName, String destFileName)
HelpASisterOut
  • 3,085
  • 16
  • 45
  • 89

2 Answers2

6

Creating a FileInfo object does not open or lock the file, so there is nothing to close or unlock. FileInfo does not implement IDisposable, so there is nothing to dispose. You don't need to do anything special, the objects will be garbage collected some time after there are no references to them.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Blackwood
  • 4,504
  • 16
  • 32
  • 41
  • They are causing me problems when I use the files in the rest of the code. I am getting the following error: the process cannot access the file because it is being used by another process – HelpASisterOut May 05 '15 at 13:44
  • 1
    @HelpASisterOut: not the `FileInfo()` is causing you problems but how you use them. But you haven't shown the code. Maybe you have opened a stream without closing it. – Tim Schmelter May 05 '15 at 13:45
  • I don't believe the problem can be that you created the `FileInfo` objects. The file must be in use for some other reason. – Blackwood May 05 '15 at 13:46
  • You are still leaving out code. What does GET_FileName do? Does it open a file? – Scott May 05 '15 at 14:53
  • @HelpASisterOut: The code that you have shown us uses `File.Delete` and `File.Move` but neither of those methods will leave the file in use. It must be something outside the code you have shown that is making the file be in use. – Blackwood May 05 '15 at 15:32
  • @scott I added the Get_FileName function. Sorry please note this is not my code. – HelpASisterOut May 06 '15 at 07:43
  • 1
    That looks like your potential issue. A FileStream is opened and is neither Closed nor Disposed. The easiest solution is to wrap your FileStream usage with a Using block. You can probably start the block when you create the FileStream and end it right after xmldoc.Load(fs) since you no longer need it after that. This is good to do with anything implementing IDisposable. On a side note, it is not good practice to eat exceptions like this code is doing and it's odd to catch SqlException when SQL Server isn't being accessed. – Scott May 06 '15 at 08:29
  • 1
    One more side note, GET_FileName is not a good method name since it does not actually return a filename. Also, this method really should be implemented as a function that returns an Integer instead of setting an Integer that is passed in ByRef. Returning a value makes the function easier to use and easier on people reading the code later because VB doesn't have any notification that a parameter being passed in is ByRef at the call site. – Scott May 06 '15 at 08:36
  • @Scott I agree with everything you said. This is somebody else's code and I am going through it and fixing it. Thanks a lot for your input. Very helpful. – HelpASisterOut May 06 '15 at 09:53
  • @Scott Hey scott please post this as an answer so I can accept it – HelpASisterOut May 07 '15 at 08:23
3

In GET_FileName, a FileStream is opened and is neither Closed nor Disposed. The easiest solution is to wrap your FileStream usage with a Using block. You can probably start the block when you create the FileStream and end it right after xmldoc.Load(fs) since you no longer need it after that. This is good to do with anything implementing IDisposable.

Scott
  • 370
  • 2
  • 7