Solved

How to make the filesystemwatcher two different folders

Posted on 2006-11-19
12
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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 …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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