I am now monitoring one folder at a time, and i am wondering how can i file watch three folders
at same time.
please see my updated codes below,,, i am wondering wether i should give
m_Watcher.Path = draft;
and m_Watcher.Path = release;
and m_Watcher.Path = archive;
these three lines or not
My codes:
Dictionary<string, FileSystemWatcher> monitor = new Dictionary<string, FileSystemWatcher>();
public void monitorFolder(string folderPath)
{
string draft = ini.ReadValue("Location", "Draft");
string release = ini.ReadValue("Location", "Release");
string archive = ini.ReadValue("Location", "Archive");
// System.IO.Directory.CreateDirectory(draft); // no need to check if exists
if (monitor.ContainsKey(draft)) return; //if directory already being monitored
if (monitor.ContainsKey(release)) return;
if (monitor.ContainsKey(archive)) return;
m_Watcher = new System.IO.FileSystemWatcher();
m_Watcher.Filter = "*.*";
m_Watcher.Path = folderPath; //give the folderpath
m_Watcher.IncludeSubdirectories = true;
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
m_Watcher.Created += new FileSystemEventHandler(OnChanged);
m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
m_Watcher.EnableRaisingEvents = true;
///Initializing delegate that we have created to update UI control outside the current thread
addItemInList = new delAddItem(this.AddString);
}
and called the monitorFolder
in my function
monitorFolder(draft);
monitorFolder(release);
monitorFolder(archive);
Example of monitoring multiple folders: First create some container to hold every FileSystemWatcher object:
Then for every folder to add, add it to the container:
Now, for every folder you would like to add, call the method:
For example to the code above:
Then it is possible to get which folder fired the event by working with the EventArgs variable in the event.