文件已被使用,因此无法访问

时间:2019-07-18 20:48:37

标签: vb.net

我有一个Sub,它读取在另一个Sub中创建的文件。我遇到错误

  

无法访问其他进程正在使用的文件

从网上阅读的内容中,我需要关闭StreamReader。我尝试在不同的变量上使用.close(),但似乎无济于事。

下面是编写另一个Sub然后访问的文件的代码。

Private Sub CreateGraphicsFunction(sender As Object, e As EventArgs)

    Dim Regex = New Regex("infoEntityIdent=""(ICN.+?)[""].*?[>]")
    strGraphicFile = MoveLocation & "\ICN-LIST.txt"

    Dim ICNFiles = Directory.EnumerateFiles(MovePath, "*.*", SearchOption.AllDirectories)

    For Each tFile In ICNFiles
        Dim input = File.ReadAllText(tFile)

        Dim match = Regex.Match(input)
        If match.Success Then
            output.Add(match.Groups(1).Value)
        End If
    Next
    File.WriteAllLines(strGraphicFile, output)
    locationGraphicsLog = strGraphicFile

End Sub

读取创建文件的另一个Sub

 Private Sub btnFindICN_Click(sender As Object, e As EventArgs) Handles btnFindICN.Click


    Application.UseWaitCursor = True
    Application.DoEvents()
    Me.Refresh()

    Dim sGraphicFilesToFind As String
    Dim graphicLocation As String
    'MoveWithPath As String
    Dim graphicFile As String

    graphicLocation = txtSearchICN.Text
    MoveLocation = MovePath

    graphicLogFile = MoveLocation & "\Reports\1-OrphanedFilesItems.txt"
    Dim FILE_NAME As String
    FILE_NAME = MoveLocation & "\ICN-LIST.txt"


    Dim objReader As New System.IO.StreamReader(FILE_NAME)
    Dim sGraphicFile As String

    Do While objReader.Peek() <> -1
        graphicFile = objReader.ReadLine()
        sGraphicFilesToFind = graphicLocation & "\" & graphicFile & "*.*"
        sGraphicFile = graphicFile
        Dim createGraphicReportFldr As String

        Dim paths() As String = IO.Directory.GetFiles(graphicLocation, sGraphicFile, IO.SearchOption.AllDirectories)
        If paths.Count = 0 Then
            'Debug.Print(graphicFile)
            If System.IO.File.Exists(graphicLogFile) = True Then
                Dim objWriter As New System.IO.StreamWriter(graphicLogFile, IO.FileMode.Append)
                objWriter.WriteLine(graphicFile)
                objWriter.Close()
            Else
                'MsgBox("Creating Orphaned graphicFile Now. ")

                createGraphicReportFldr = MoveLocation & "\Reports"
                If Not IO.Directory.Exists(createGraphicReportFldr) Then
                    IO.Directory.CreateDirectory(createGraphicReportFldr)
                    'MsgBox("folder created" & createGraphicReportFldr)

                    Dim writeFile As IO.StreamWriter
                    writeFile = IO.File.CreateText(graphicLogFile)
                    writeFile.Write(graphicFile & vbCrLf)
                    writeFile.Close()
                Else
                    'MsgBox("Folder already exist")
                End If
            End If
        Else
            For Each pathAndFileName As String In paths
                Dim createGraphicsFolder As String
                'Dim moveFileToNewFolder As String

                If System.IO.File.Exists(pathAndFileName) = True Then
                    Dim sRegLast As String = pathAndFileName.Substring(pathAndFileName.LastIndexOf("\") + 1)
                    Dim toGraphiicFileLocation As String

                    'MsgBox("sRegLast " & sRegLast)
                    fileGraphicLoc = MoveLocation & sRegLast
                    createGraphicsFolder = MoveLocation & "\Figures"
                    moveGraphicFileToNewFolder = MoveLocation & "\Figures\" & sRegLast
                    toGraphiicFileLocation = createGraphicsFolder & "\" & sRegLast
                    'MsgBox("FileLoc " & fileLoc)

                    If Not IO.Directory.Exists(createGraphicsFolder) Then
                        IO.Directory.CreateDirectory(createGraphicsFolder)
                        ' MsgBox("folder created" & createGraphicsFolder)
                    End If

                    If System.IO.File.Exists(fileGraphicLoc) = False Then
                        System.IO.File.Copy(pathAndFileName, moveGraphicFileToNewFolder)
                        Debug.Write("Graphics moved to : " & moveGraphicFileToNewFolder & vbCrLf)
                    End If
                End If

            Next
        End If
    Loop
    'MsgBox("graphicFiles have been moved")

    Call CreateGraphicsFunction(Nothing, System.EventArgs.Empty)
    Application.UseWaitCursor = False
    Application.DoEvents()
    ' Me.Close()
End Sub

1 个答案:

答案 0 :(得分:1)

在“其他子”中,更改

Dim objReader As New System.IO.StreamReader(FILE_NAME)

Using objReader = New System.IO.StreamReader(FILE_NAME)

并在完成后添加End Using。大概在Loop之后。这将确保一次性流始终得到处理。

请参见Using Statement (Visual Basic)。如果您能够将IDisposable对象的范围限制为一个方法,那么您几乎总是希望将IDisposable对象包装在Using块中。