FileSystemWatcher is not working

后端 未结 2 1767
长发绾君心
长发绾君心 2021-01-21 14:15

I added FileSystemWatcher in Form1_Load like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase         


        
2条回答
  •  醉梦人生
    2021-01-21 15:08

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim watcher As New IO.FileSystemWatcher()
    
    'For watching current directory
    watcher.Path = **System.IO.Directory.GetCurrentDirectory()** 'Note how to obtain current directory
    watcher.NotifyFilter = NotifyFilters.LastWrite
    
    'When I pasted your code and created my own status.txt file using 
    'right click->new->Text File on Windows 7 it appended a '.txt' automatically so the
    'filter wasn't finding it as the file name was status.txt.txt renaming the file
    'solved the problem
    watcher.Filter = "status.txt" 
    
    AddHandler watcher.Changed, AddressOf OnChanged
    
    watcher.EnableRaisingEvents = True
    End Sub
    
    Private Shared Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
    MessageBox.Show("Got it")
    End Sub
    

    From http://bartdesmet.net/blogs/bart/archive/2004/10/21/447.aspx

    You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up

提交回复
热议问题