我想创建一个应用程序,而不是只查看根层次结构文件夹,......我单独查看每个文件夹(关闭监视子文件夹)
但是,我似乎无法正确获取删除逻辑。我希望能够删除层次结构中的任何文件夹,无论是在应用程序内部,还是在外部,比如通过 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;