首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >释放FileInfo()

释放FileInfo()
EN

Stack Overflow用户
提问于 2015-05-05 13:32:35
回答 2查看 4K关注 0票数 1
代码语言:javascript
复制
 Dim FolderName As New IO.DirectoryInfo(FileLocationFolder)
 Dim diar1 As IO.FileInfo() = FolderName.GetFiles("*.xml")

我正在循环遍历diar1,如下所示

代码语言:javascript
复制
 For Each CurrFile In diar1
 Next 

那能锁定那个目录中的所有文件吗?

如果是的话,我做完后该如何处理呢?任何帮助都将不胜感激。

下面是我剩下的代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

此函数中发生错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-07 15:14:30

在GET_FileName中,一个FileStream是打开的,既不关闭也不释放。最简单的解决方案是用一个Using块包装您的FileStream使用。您可能可以在创建FileStream时启动该块,并在xmldoc.Load(fs)之后结束它,因为在此之后不再需要它。这与实现IDisposable的任何事情都有很好的关系。

票数 3
EN

Stack Overflow用户

发布于 2015-05-05 13:37:09

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30054477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档