参数异常(路径不是合法形式)

发布于 2024-11-30 20:54:22 字数 1122 浏览 3 评论 0原文

背景信息:我正在使用在服务中实现的 FileSystemWatcher 类来监视文件中的更改。这是触发 onCreate 事件时抛出参数异常(Path 不是合法形式)的代码部分。

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;
      }
    }
  }

问题: 谁能告诉我为什么抛出异常以及如何解决它?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹊巢 2024-12-07 20:54:22

几周前我做我的事情时遇到了同样的问题。
我发现您需要在设置其他任何内容之前设置路径。
因此,在声明对象之后:

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

您可以在此处阅读更多内容:
为 Windows 创建文件监视服务

我希望这有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文