首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileSystemWatcher ArgumentException

FileSystemWatcher ArgumentException
EN

Stack Overflow用户
提问于 2016-03-20 03:09:20
回答 1查看 1.1K关注 0票数 3

我很难理解FileSystemWatcher应该如何工作。我试图让我的代码等待一个文件的存在,然后调用另一个函数。我的代码如下:

字符串path2 = @"N:\reuther\TimeCheck\cavmsbayss.log";

代码语言:javascript
复制
        FileSystemWatcher fw = new FileSystemWatcher(path2);
       fw.Created += fileSystemWatcher_Created;

然后,我有一个独立的函数,一旦文件的事件被调用,就应该处理它:

代码语言:javascript
复制
        static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
    {
        MessageBox.Show("Ok im here now");
    }

但是它

目录名N:\reuther\TimeCheck\cavmsbayss.log无效。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-20 03:17:53

根据文档path参数指示:

用标准或通用命名公约(UNC)符号来监视的目录。

将路径传递给目录,而不是特定的文件:

代码语言:javascript
复制
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属性,注意该文件的创建:

代码语言:javascript
复制
static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
    if (e.Name == "cavmsbayss.log")
    {
        MessageBox.Show("Ok im here now");
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36109819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档