Solved

How to make the filesystemwatcher two different folders

Posted on 2006-11-19
12
354 Views
Last Modified: 2011-10-03
How to make the filesystemwatcher watch two different folder

The first path is c:\FolderTowatch1

And the other is d:\FolderTowatch2


Do I need to create two differrent filesystemwatcher?

What if I want to watch many files or folder? How can I do this?

Thanks

Jack
0
Comment
Question by:JackOfPH
  • 7
  • 5
12 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17977164
if you want to watch 2 different folders you would need 2 FileSystemWatcher components, unless one of the folders is contained inside the other, in which case you would only monitor the top lever folder only (provided you set the IncludeSubdirectories to TRUE).
any FSW will monitor every file within the parent folder, so the watching many files part you need is already covered. you can set which types of files you want by setting the FILTER property. by default it watches all files within the directory.
0
 
LVL 15

Author Comment

by:JackOfPH
ID: 17977195
Okey, I get your point, but how can I make it flexible? what I mean is
I am creating a program that will allow the user to select which folder they want to monitor.
What if the user selects 3 folders or 5 folders to watch . How can I create new 3 filesystemwatcher or 5 filesystemwatcher to watch for that particular folders?

Any suggestion would helpful.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17977225
i dont know how you are making your users select the folders to watch and files to watch. if you use folderbrowser component, you cant select files. and if you use openfiledialog component you cant select folders... what is your method?
0
 
LVL 15

Author Comment

by:JackOfPH
ID: 17977382
Put all the folders (including its path) in the listbox. All the items in the listbox is the one that the filesystemwatcher will watch.
0
 
LVL 15

Author Comment

by:JackOfPH
ID: 17977410
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17977465
that seems to be what you need. do you need help translating the C# code?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Author Comment

by:JackOfPH
ID: 17977488
Is there anyother way to do this?
0
 
LVL 15

Author Comment

by:JackOfPH
ID: 17977489
is this right?

Private _watchers As New ArrayList()

    Protected Sub WatchRootDirectory(ByVal root As String)
        ' Create filesystemwatcher and set property values.
        Dim watcher As New FileSystemWatcher(root)
        watcher.IncludeSubdirectories = True
        watcher.EnableRaisingEvents = True
        watcher.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName

        ' Wire events.
        AddHandler watcher.Changed, AddressOf watcher_Changed
        AddHandler watcher.Created, AddressOf watcher_Created
        AddHandler watcher.Deleted, AddressOf watcher_Deleted

        ' Add to collection.
        _watchers.Add(watcher)
    End Sub 'WatchRootDirectory


    Private Sub watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
        MessageBox.Show(Me, e.FullPath + " changed.")
    End Sub 'watcher_Changed


    Private Sub watcher_Created(ByVal sender As Object, ByVal e As FileSystemEventArgs)
        MessageBox.Show(Me, e.FullPath + " created")
    End Sub 'watcher_Created


    Private Sub watcher_Deleted(ByVal sender As Object, ByVal e As FileSystemEventArgs)
        MessageBox.Show(Me, e.FullPath + " deleted.")
    End Sub 'watcher_Deleted
0
 
LVL 15

Author Comment

by:JackOfPH
ID: 17977502
Is my translation is right?
0
 
LVL 13

Accepted Solution

by:
newyuppie earned 500 total points
ID: 17977578
<<Is my translation is right?>>
it would appear so

<<Is there anyother way to do this?>>
as i said, i believe you need to instantiate a new watcher for every directory that is not nested into the root directory. if you have selected a dir, and its has subdirs, it will watch them too so no need to duplicate those ones.
actually i think that example code is a clever solution.


only thing you need now is to loop through the listbox's items and add the watchers one by one for every folder there. something maybe like:

for each obj as Object in ListBox1.Items
    WatchRootDirectory(obj.ToString())
next
0
 
LVL 15

Author Comment

by:JackOfPH
ID: 17978227
Thanks,

0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17980016
thank you for the points
NY
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now