我很难理解FileSystemWatcher应该如何工作。我试图让我的代码等待一个文件的存在,然后调用另一个函数。我的代码如下:
字符串path2 = @"N:\reuther\TimeCheck\cavmsbayss.log";
FileSystemWatcher fw = new FileSystemWatcher(path2);
fw.Created += fileSystemWatcher_Created;然后,我有一个独立的函数,一旦文件的事件被调用,就应该处理它:
static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
MessageBox.Show("Ok im here now");
}但是它
目录名N:\reuther\TimeCheck\cavmsbayss.log无效。
发布于 2016-03-20 03:17:53
根据文档,path参数指示:
用标准或通用命名公约(UNC)符号来监视的目录。
将路径传递给目录,而不是特定的文件:
string pathToMonitor = @"N:\reuther\TimeCheck";
FileSystemWatcher fw = new FileSystemWatcher(pathToMonitor);
fw.EnableRaisingEvents = true; // the default is false, you may have to set this too
fw.Created += fileSystemWatcher_Created;然后,使用Name类中的FullPath属性或FileSystemEventArgs类中的FullPath属性,注意该文件的创建:
static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
if (e.Name == "cavmsbayss.log")
{
MessageBox.Show("Ok im here now");
}
}https://stackoverflow.com/questions/36109819
复制相似问题