被監視我想創造一個而不是僅看一個根層級文件夾,......我單獨看每個文件夾(與腕錶的子文件夾關閉)的應用程序刪除文件夾中FileSystemWatcher的
然而,我似乎無法獲得正確的刪除邏輯。我希望能夠刪除層次結構中的任何文件夾,可以在應用程序內部刪除,也可以通過Windows資源管理器在外部刪除。
當我嘗試其中的任何一種時,看起來會出現鎖定問題,因爲刪除命令無法執行。
如果我禁用了看,那麼一切似乎工作。所以我假設問題是試圖刪除正在觀看的文件夾。
有沒有辦法解決這個問題?理想情況下,我喜歡fileSystemWatcher自動停止觀看其觀看文件夾被刪除的時間。
public MainWindow()
{
InitializeComponent();
fsw1 = new FileSystemWatcher()
{
Path = "Folder",
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.DirectoryName,
IncludeSubdirectories = false,
Filter = "",
EnableRaisingEvents = true
};
fsw1.Deleted += new FileSystemEventHandler(OnFileSystemItemDeleted);
fsw2 = new FileSystemWatcher()
{
Path = "Folder/Folder",
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.DirectoryName,
IncludeSubdirectories = false,
Filter = "",
EnableRaisingEvents = true
};
fsw2.Deleted += new FileSystemEventHandler(OnFileSystemItemDeleted);
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
//fsw1.EnableRaisingEvents = false;
//fsw2.EnableRaisingEvents = false;
System.IO.Directory.Delete("Folder", true);
}
void OnFileSystemItemDeleted(object sender, FileSystemEventArgs e)
{
}
FileSystemWatcher fsw1, fsw2;
你的意思是停止觀看,當它即將被刪除? –
只要首先處理觀察者,沒有任何意義。看着被刪除的文件夾需要看父目錄。 –
@TonyHopkinson,我想是的,是的。 – hammer