Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to make the filesystemwatcher two different folders

Posted on 2006-11-19
12
Medium Priority
?
360 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

704 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