我使用FileSystemWatcher来监视几个文件夹。当它触发更改事件时,我希望获得已更改文件的文件名。因为当我尝试使用e.Name或e.FullPath时,观察者正在监视文件夹,所以我得到了文件夹路径。
有办法得到文件名吗?
代码:它是一组观察者。
watchers[_Idx] = new FileSystemWatcher();
watchers[_Idx].Path = row.Cells[0].Value.ToString();
watchers[_Idx].IncludeSubdirectories = true;
watchers[_Idx].NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.DirectoryName | NotifyFilters.FileName |
NotifyFilters.Size;
watchers[_Idx].Changed += new FileSystemEventHandler(SyncThread);
watchers[_Idx].Created += new FileSystemEventHandler(SyncThread);
watchers[_Idx].Renamed +=new RenamedEventHandler(SyncThread);
watchers[_Idx].EnableRaisingEvents = true;发布于 2014-07-05 14:15:26
您可以从事件中提取文件名:
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}摘自这里的代码片段。
https://stackoverflow.com/questions/24586470
复制相似问题