Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Ok I have successfully achieved my task of monitoring all hard drives on my computer with filesystem watcher but now the problem is that it will not stop or exit my for each loop with the conventional method "filesystemwatcher1.enableraisingevents = false"

here is the full code

VB
 Imports System.IO
Public Class Form1
    Private Sub OnChanged(ByVal sender As Object, ByVal e As FileSystemEventArgs)
        ' Specify what is done when a file is changed, created, or deleted.
        ListBox1.Items.Add(e.FullPath & " " & e.ChangeType)
        Label6.Text = (e.FullPath & " " & e.ChangeType)
        Label1.Text += 1
    End Sub
    Private Sub OnRenamed(ByVal sender As Object, ByVal e As RenamedEventArgs)
        ' Specify what is done when a file is renamed.
        ListBox1.Items.Add(e.FullPath & " " & e.ChangeType)
        Label6.Text = (e.FullPath & " " & e.ChangeType)
        Label1.Text += 1
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
        FSW()
    End Sub
    Public Sub FSW()
        Dim drives As String() = Environment.GetLogicalDrives()
        For Each strDrive As String In drives
            'Check if the drive is ready to be used 
            Dim df As New DriveInfo(strDrive)
            If Not df.IsReady Then
                Continue For
            End If
            Dim _watcher As New FileSystemWatcher()
            _watcher.IncludeSubdirectories = True
            _watcher.Path = strDrive
            _watcher.NotifyFilter = NotifyFilters.Attributes Or NotifyFilters.CreationTime Or NotifyFilters.DirectoryName Or NotifyFilters.FileName _
            Or NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.Security Or NotifyFilters.Size
            AddHandler _watcher.Changed, AddressOf OnChanged
            AddHandler _watcher.Created, AddressOf OnChanged
            AddHandler _watcher.Deleted, AddressOf OnChanged
            AddHandler _watcher.Renamed, AddressOf OnRenamed
                _watcher.EnableRaisingEvents = True
        Next
    End Sub
End Class



How can i rearrange this so that I can start and stop the filesystem watcher?

thank you in advance!!
Posted

1 solution

You can use one of at least two approaches, of different degree of "lightness".

  • As you add some named methods to the invocation lists of event instances of FileSystemWatcher, you can also remove them. In VB.NET, a handler is added to an invocation list of some event instance using AddHandler, removed using RemoveHandler (same as C# '+=' and '-='). Please see, for example:
    http://www.thescarms.com/dotnet/EventHandler.aspx[^].
  • Use your handler method as they are already defined. Add a Boolean field in your declaring class (form, in your case), such as allowHandling. Use it as a flag: add a check of this flag in every handler method; if the flag is false, do nothing. This way, assigning the value false to this flag will pause handling, true will resume it. Simple, isn't it?


—SA
 
Share this answer
 
Comments
Dale 2012 14-Jan-13 5:35am    
thank you I decided to go with your second approach and it worked like a charm!!!

that was easy!
Sergey Alexandrovich Kryukov 14-Jan-13 11:14am    
You are very welcome.

By the way, the benefit of second approach is that you don't have to have named methods. Anonymous methods are much better in most cases.

Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900