Sorry, I'm not sure about the best title for this, but I hope everybody understands.
I want to have code like this
foreach (var path in locationsToMonitor)
{
MessageBox.Show(path.ToString());
watchPath(path, fileWatchNumber);
fileWatchNumber ++;
}
private void watchPath(string path, short fileWatchNumber)
{
string fileSystemWatcherName = "FileMonitor" + fileWatchNumber.ToString();
FileSystemWatcher fileSystemWatcherName = new FileSystemWatcher();
FileMonitor.SynchronizingObject = this;
FileMonitor.Path = locationsToMonitor.First();
FileMonitor.IncludeSubdirectories = true;
FileMonitor.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
FileMonitor.Filter = "*.*";
FileMonitor.Changed += new FileSystemEventHandler(this.FileMonitor_Changed);
FileMonitor.Created += new FileSystemEventHandler(this.FileMonitor_Changed);
FileMonitor.Renamed += new RenamedEventHandler(FileMonitor_OnRenamed);
FileMonitor.EnableRaisingEvents = true;
}
Basically, I would like to initiate
new FileSystemWatcher();
with whatever the value of my string fileSystemWatcherName is so that I can dynamically create new fileSystemWatchers based on how many paths are listed within a text file.
so, it would create FileMonitor1, FileMonitor2, etc.
UPDATE:
The reason why I would want to create them dynamically is that what I want my program to do is to open a text file as an array. For each newline item in that text file, I would like to create a fileSystemWatcher. The only thing is, is that I don't know whether there will be one line in the file or 50 lines.
I read the post about using dictionaries, but I'm not quite sure how to apply it here.
I tried this, but, according to the compiler it is invalid code.
Dictionary<string, string> fileSystemWatchers = new Dictionary<string, string>();
fileSystemWatchers.Add("fileMonitor" + num.ToString(), "");
FileSystemWatcher fileSystemWatchers["fileMonitor1"] = new FileSystemWatcher();