多次调用 FileSystemWatcher 事件
FileSystemWatcher events called multiple times
我使用 filesystemwatcher 创建了一个控制台应用程序来监视特定文件夹。
当我在任何文件中进行任何更改或创建、重命名或删除时,它将触发一个事件。
但是当我更改文件时,2 个事件正在变得 triggered.I 只需要为相应的操作触发一个事件....
下面是我更改文件时触发 2 个事件的代码
public static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
string time = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
string myFilePath = e.FullPath;
string ext = Path.GetExtension(myFilePath);
if (ext == ".tmp" || ext == ".TMP" || ext == "")
{
}
else
{
string date = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("A new file has been changed - " + e.Name + " " + "on" + " " + date + " File Changed", Console.ForegroundColor);
// SendAttachmentMail("A File Has Been Changed - " + e.Name + "", date, "File Changed");
//File_Date = date;
//watcher.Dispose();
}
}
这种行为是documented。还记录的是,您不能保证所有事件都将交付。应用程序必须保护自己不受任何一种情况的影响。
Common file system operations might raise more than one event. For
example, when a file is moved from one directory to another, several
OnChanged and some OnCreated and OnDeleted events might be raised.
Moving a file is a complex operation that consists of multiple simple
operations, therefore raising multiple events. Likewise, some
applications (for example, antivirus software) might cause additional
file system events that are detected by FileSystemWatcher.
另请注意,以上是 Win32 行为,FSW 只是将操作系统功能封装到一个更好的界面中。
我使用 filesystemwatcher 创建了一个控制台应用程序来监视特定文件夹。 当我在任何文件中进行任何更改或创建、重命名或删除时,它将触发一个事件。 但是当我更改文件时,2 个事件正在变得 triggered.I 只需要为相应的操作触发一个事件....
下面是我更改文件时触发 2 个事件的代码
public static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
string time = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
string myFilePath = e.FullPath;
string ext = Path.GetExtension(myFilePath);
if (ext == ".tmp" || ext == ".TMP" || ext == "")
{
}
else
{
string date = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("A new file has been changed - " + e.Name + " " + "on" + " " + date + " File Changed", Console.ForegroundColor);
// SendAttachmentMail("A File Has Been Changed - " + e.Name + "", date, "File Changed");
//File_Date = date;
//watcher.Dispose();
}
}
这种行为是documented。还记录的是,您不能保证所有事件都将交付。应用程序必须保护自己不受任何一种情况的影响。
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.
另请注意,以上是 Win32 行为,FSW 只是将操作系统功能封装到一个更好的界面中。