Solved

How to make the filesystemwatcher two different folders

Posted on 2006-11-19
12
353 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

17 Experts available now in Live!

Get 1:1 Help Now