Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument Exception (Path is not of a legal form)

Background Infomation: I am using FileSystemWatcher class implemented in a service to monitor changes in the files. Heres the section of coding that throws an Argument Exception (Path is not a legal form) when the onCreate event is triggered.

FileMonitor.CS

public partial class FileMonitor:ServiceBase
{
 public FileSystemWatcher Watcher = new FileSystemWatcher();

    Private void FileWatcher()
    {
    FileActionHandler ActionHandler = new FileActionHandler();
    Watcher.Created += new FileSystemEventHandler(ActionHandler.onCreate);
    Watcher.Deleted += new FileSystemEventHandler(ActionHandler.onDelete);
    Watcher.Renamed += new RenamedEventHandler(ActionHandler.onRenamed);
    Watcher.EnableRaisingEvents = true;
    }
}

FileActionHandler.CS

 class FileActionHandler
 {
  FileMonitor FileMon = new FileMonitor();
  public void onCreate/onRename/onDelete(object source, FileSystemEventArgs e)
    {
      try
      {
       FileMon.Watcher.EnableRaisingEvents = false;
      }
      catch
      {
       /* Exception Code */
      }
      finally
      {
       FileMon.Watcher.EnableRaisingEvents = true;
      }
    }
  }

Question: Can anyone advice me on why is the exception being thrown and how I can go about resolving it ?

like image 805
Derek Avatar asked Oct 24 '25 13:10

Derek


1 Answers

I ran into the same issue when I did mine a few weeks ago. What I found was that you need to set the path before you set anything else. So, right after you declare the object:

FileSystemWatcher watchfolder = new FileSystemWatcher();
watchfolder.Path = ConfigurationManager.AppSettings["MonitorPath"];

You can read more here: Create a file watcher service for windows

I hope this helps

like image 84
Savage Avatar answered Oct 27 '25 01:10

Savage