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

我正在循环通过 diar1,如下所示

 For Each CurrFile In diar1
 Next 

这会锁定该目录中的所有文件吗?

如果是这样,我完成后如何处理它?任何帮助,将不胜感激。

这是我的其余代码:

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

.

此函数出现错误:

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)
4

2 回答 2

6

创建FileInfo对象不会打开或锁定文件,因此无需关闭或解锁。FileInfo 没有实现IDisposable,所以没有什么可处置的。你不需要做任何特别的事情,对象将在没有引用它们的一段时间后被垃圾收集。

于 2015-05-05T13:37:09.203 回答
3

在 GET_FileName 中,FileStream 被打开,既不是 Closed 也不是 Disposed。最简单的解决方案是使用 Using 块包装您的 FileStream 使用情况。您可能可以在创建 FileStream 时开始该块并在 xmldoc.Load(fs) 之后立即结束它,因为之后您不再需要它。这对任何实现 IDisposable 的东西都有好处。

于 2015-05-07T15:14:30.610 回答