Home > database >  How to pass arguments in cmd to FileSystemWatcher?
How to pass arguments in cmd to FileSystemWatcher?

Time:10-19

I'm trying to pass arguments through the command line to monitor folders with How to pass arguments in cmd to FileSystemWatcher but I'm not getting it.

What I want is to be able to pass the command directly into the CMD, for example:

monitor.exe C/:path/monitor

code:

static void Main(string[] args)
        {

             Console.WriteLine("Path: ");
             string Path = Console.ReadLine();
         
            using var watcher = new FileSystemWatcher(Path);

can you help me?

CodePudding user response:

You can do it like this.

put your path in quotes if it contains spaces. ("c:/some path/path") otherwise this is fine

monitor.exe C/:path/monitor

then in C#

static void Main(string[] args)
{
      var path =  args[0];  
      Console.WriteLine($"Path: {path}");
         
      using var watcher = new FileSystemWatcher(path);
      {
           //do logic here with path
      }
}

obviously add some error checking and what to ensure the path is present, but this should get you started. Also with more than one path the args[0] will need to change, but this again should get you started.

  •  Tags:  
  • c#
  • Related