【发布时间】:2014-05-21 11:40:12
【问题描述】:
我遇到了一个奇怪的问题。我搜索并尝试解决此问题,但其他解决方案对我不起作用。我在看一个文件夹。当在该文件夹中创建任何文件时,创建的事件会调用。但奇怪的是,它被称为该文件夹中存在的文件数。
例如:
对于第一个文件,它只调用一次(我想要的)。
对于第二个文件,它调用了两次。
对于第三个文件,它被调用了 3 次。
代码:
fw.Path = @"D:\xx\xx\" + fullName.GetStringPart(0, 3) + @"\" + fullName.GetStringPart(3, 2) + @"\" + fullName.Substring(5, 4) + @"\" + fullName;
fw.Filter = "*.*";
fw.NotifyFilter = NotifyFilters.CreationTime|
NotifyFilters.FileName;
fw.Changed += new FileSystemEventHandler(OnFileChanged);
fw.Created += new FileSystemEventHandler(OnCreated);
fw.Deleted += new FileSystemEventHandler(OnDeleted);
fw.EnableRaisingEvents = true;
事件:
void OnCreated(object sender, FileSystemEventArgs e)
{
try
{
fw.EnableRaisingEvents = false;
while (!TestOpen(e.FullPath)) ;
string str = e.FullPath;
allImag.Add(str.Replace("Thumbs", "images"));
allThumbImag.Add(e.FullPath);
if (InvokeRequired)
this.Invoke(new Action(() => this.addImage(e.FullPath)));
else
addImage(e.FullPath);
}
finally
{
fw.EnableRaisingEvents = true;
}
}
你猜到了吗?
【问题讨论】:
-
你能发布你的代码吗?
-
如何创建文件?也许程序也会尝试对现有文件做一些事情?
-
我只是在移动文件。
-
@Rashad 只是预感,您是否尝试过禁用防病毒软件?
-
这些活动您只注册一次吗?
标签: c# .net winforms filesystemwatcher