参数异常(路径不是合法形式)
背景信息:我正在使用在服务中实现的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几周前我做我的事情时遇到了同样的问题。
我发现您需要在设置其他任何内容之前设置路径。
因此,在声明对象之后:
您可以在此处阅读更多内容:
为 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:
You can read more here:
Create a file watcher service for windows
I hope this helps