背景信息:我正在使用服务中实现的FileSystemWatcher类来监视文件中的更改。下面是触发onCreate事件时抛出Argument Exception(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;
}
}
}
问题: 任何人都可以告诉我为什么抛出异常以及如何解决它?
答案 0 :(得分:7)
FileSystemWatcher watchfolder = new FileSystemWatcher();
watchfolder.Path = ConfigurationManager.AppSettings["MonitorPath"];
您可以在这里阅读更多内容: Create a file watcher service for windows
我希望这会有所帮助