Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
 
 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 13-Jan-13 20:58pm
Dale 20121.8K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Dale 2012 at 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 at 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)

  Print Answers RSS
0 DamithSL 335
1 OriginalGriff 209
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Maciej Los 123
0 OriginalGriff 7,854
1 DamithSL 5,949
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,084
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100