1 回答

TA貢獻2003條經驗 獲得超2個贊
我正在維護一個與您的要求具有相似功能的項目。
在該功能中,我們使用FileSystemWatcher來監控特定 UNC 位置的各種操作。您可以實施OnError將在 UNC 路徑不可用時觸發的事件。
您可以查看上面的鏈接了解詳細信息,這里仍然是一個簡短的示例
using (FileSystemWatcher watcher = new FileSystemWatcher(@"\\your unc path"))
{
// Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories.
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
watcher.Created += (s, e) => { Console.WriteLine($"Created {e.Name}"); };
watcher.Deleted += (s, e) => { Console.WriteLine($"Deleted {e.Name}"); };
watcher.Error += (s, e) => { Console.WriteLine($"Error {e.GetException()}"); };
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press 'q' to quit the sample.");
while (Console.Read() != 'q') ;
}
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報