Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have asked this question and do not mean to spam in any way to ask again but since then I have made an attempt to figure this thing out. To refresh my question I am looking to monitor all hard drives directories and sub directories with Filesystem watcher. Thus far i have this code which does not seem to monitor any drive


VB
Imports System.IO
Public Class Form1
    Private _watchers As FileSystemWatcher()
    Private Sub OnStart(args As String())
        Dim drives As String() = Environment.GetLogicalDrives()
        _watchers = New FileSystemWatcher(drives.Length - 1) {}
        Dim i As Integer = 0
        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.Path = strDrive
            AddHandler _watcher.Changed, AddressOf OnChanged
            AddHandler _watcher.Created, AddressOf OnChanged
            AddHandler _watcher.Deleted, AddressOf OnChanged
            AddHandler _watcher.Renamed, AddressOf OnRenamed
        Next
    End Sub
    Private Sub OnChanged(source As Object, e As FileSystemEventArgs)
        ' Specify what is done when a file is changed, created, or deleted.
        ListBox1.Items.Add("File: " & e.FullPath & " " & e.ChangeType)
    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("File: " & e.FullPath & " " & e.ChangeType)
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FolderWatcherTest.EnableRaisingEvents = True
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        FolderWatcherTest.EnableRaisingEvents = False
    End Sub
End Class



Some of this code has been converted from C# to vb.net
what do i need to correct or add to this to get the desired result?

thank you in advance!!
Posted
Comments
Sandeep Mewara 13-Jan-13 2:12am    
You get errors? Wheres the C# source?
_Vitor Garcia_ 14-Jan-13 4:23am    
I guess you should set :
.Filter = "*.*" 'for all file types
.IncludeSubdirectories = true ' or false depending on what you want to achive
.EnableRaisingEvents = true
for each of your fs objects.

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