Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FileSystemWatcher激发文件夹中尽可能多的文件_C#_Filesystemwatcher - Fatal编程技术网

C# FileSystemWatcher激发文件夹中尽可能多的文件

C# FileSystemWatcher激发文件夹中尽可能多的文件,c#,filesystemwatcher,C#,Filesystemwatcher,我在FileSystemWatch中遇到了一些奇怪的行为。下面是我的C#代码: 我的事件处理程序如下所示: static void fs_Changed(object sender, FileSystemEventArgs e) { MessageBox.Show("Folder changed", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); } 没有什么是或多或少的 当我运行窗口窗体应用程序时,

我在FileSystemWatch中遇到了一些奇怪的行为。下面是我的C#代码:

我的事件处理程序如下所示:

static void fs_Changed(object sender, FileSystemEventArgs e)
{
    MessageBox.Show("Folder changed", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
没有什么是或多或少的

当我运行窗口窗体应用程序时,它会监视文件夹。据推测,在我的文件夹中,我确实有3个现有文件。现在,我又添加了一个文件,MessageBox.Show(…)调用4次????!!!!基本上,它调用该目录中的文件数的倍

我做错什么了吗?对我来说这看起来很基本,我想知道为什么我会接到4个电话


问题:有没有办法让它只打一次电话?有解决方法吗?

您确定不只是为同一个文件获取多个事件吗?例如一个
创建的
然后一个
更改的
?我是肯定的,大卫。只需右键单击并创建一个虚拟文本文件。但是,这些事件会触发目录中的文件数!!!!这太奇怪了。请告知。您是否在每个事件中检查了
e.ChangeType
e.Name
,以查看实际发生的情况?
static void fs_Changed(object sender, FileSystemEventArgs e)
{
    MessageBox.Show("Folder changed", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}