0

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();
mberna
  • 313
  • 5
  • 18
  • Maybe just keep a List, and have your watchPath method create a new one and add it to the list. Get rid of FileMonitor( I assume it is a property on the class, but I can't see the code ). – Derek Jul 10 '17 at 17:27
  • It sounds like you're asking "How can I dynamically generate variable names?". The question is, *why* do you want to do this? Do you plan on referencing them later? Wouldn't it be simpler to just add the `FileWatcher` instances to a list, or array, and then reference them by index? – Rufus L Jul 10 '17 at 17:27
  • You cannot define variable names at runtime. Variable names are strictly a compile-time construct. To refer to objects by sequential index, one typically uses an array. To refer to objects by arbitrary value, including some arbitrary `string` value`, a dictionary is typically used. See marked duplicate. – Peter Duniho Jul 10 '17 at 17:36
  • 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. – mberna Jul 10 '17 at 17:59

0 Answers0