vb该进程无法访问该文件,因为该文件正由另一个进程使用

时间:2017-05-21 07:44:40

标签: vb.net

我在创建文件后尝试关闭:Create(LogFile).Close()但它仍无效。错误在这一行:

        My.Computer.FileSystem.WriteAllText("C:\Users\AsrahLim\Desktop\LogFile.txt", s, True)

下面是我的完整代码:

 Dim Path As String = "C:\Users\AsrahLim\"
            Dim target As String = "C:\Users\AsrahLim\Google Drive"

            If e.FullPath <> target Then
                Dim LogFile As String = "C:\Users\AsrahLim\Desktop\LogFile.txt"
            If Not Exists(LogFile) Then
                Create(LogFile).Close()
            End If
             End If

 If e.ChangeType = IO.WatcherChangeTypes.Created Then
                WriteLine("File has been created- " & e.FullPath & vbCrLf)
            FileClose()
            End If

            If e.ChangeType = IO.WatcherChangeTypes.Changed Then
                WriteLine("File has been changed- " & e.FullPath & vbCrLf)
            FileClose()
        End If

            If e.ChangeType = IO.WatcherChangeTypes.Renamed Then
            WriteLine("File has been renamed- " & e.FullPath & vbCrLf)
            FileClose()
            End If

        Dim s As String = ReadLines()

 Public Function ReadLines() As String
            Dim s As String = Console.ReadLine()
            My.Computer.FileSystem.WriteAllText("C:\Users\AsrahLim\Desktop\LogFile.txt", s, True)
        Return s

        End Function

        Public Sub WriteLine(ByVal s As String)
            Console.WriteLine(s)
        My.Computer.FileSystem.WriteAllText("C:\Users\AsrahLim\Desktop\LogFile.txt", s, True)

        End Sub

FileSystemWatcher的代码:

Public watcher As FileSystemWatcher
    Sub Main()
    Do


        'Watch Files
        Dim watcher As New System.IO.FileSystemWatcher("C:\Users\AsrahLim\", "*.docx")


        ' Add event handlers.
        AddHandler watcher.Changed, AddressOf OnChanged
        AddHandler watcher.Created, AddressOf OnChanged
        AddHandler watcher.Renamed, AddressOf OnChanged


        With watcher
            ' Begin watching.
            watcher.EnableRaisingEvents = True
            watcher.IncludeSubdirectories = True
            watcher.WaitForChanged(WatcherChangeTypes.Created Or WatcherChangeTypes.Changed Or WatcherChangeTypes.Renamed)

            watcher.Filter = "*.docx"
            ' Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. 
            watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)


        End With
    Loop Until False
    End Sub

0 个答案:

没有答案
相关问题