Solved

How to make the filesystemwatcher two different folders

Posted on 2006-11-19
12
355 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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 …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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 …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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