[RESOLVED] Filesystem Notification
Hi, I have an explorer-like interface which in many ways mimiques the behaviour of a standard explorer window. What I need is to find some way of knowing if something had changed in the filesystem since the last time I checked.
What I mean, for example if a user creates a directory or renames a file - I need a way to know that. Right now I re-read the contents of the displayed folder once per second and I added a manual 'Refresh' button, but I wonder if there is a better solution.
I tried to use FileSystemWatcher component for that but either I couldn't figure out how it works or it doesn't have such functionality.
Re: Filesystem Notification
If you want to monitor a folder/file then FileSystemWatcher is the most appropriate class to use.
I assume you forgot to set EnableRaisingEvents = True due to which you are not getting the events.
vb.net Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' set EnableRaisingEvents to True so that events are raised when something changes.
FileSystemWatcher1.EnableRaisingEvents = True
'' the folder we want to monitor for example.
FileSystemWatcher1.Path = "C:\temp"
End Sub
Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
'this event is fired when file contents is changed etc.
End Sub
Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
'this event is fired when file/folder is created.
End Sub
Private Sub FileSystemWatcher1_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted
'this event is fired when file/folder is deleted.
End Sub
Private Sub FileSystemWatcher1_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
'this event is fired when file/folder is renamed.
End Sub