Вот некоторый код, использующий FileWatcher. То, что мы хотим сделать, это
await Utils.DeleteDirectoryAsync("c:\temp\foo", recurse: true);
ниже это реализует
using System;
using System.IO;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
namespace Utils
{
internal class FileWatcher : IDisposable
{
readonly FileSystemWatcher _Watcher;
public Subject<FileSystemEventArgs> Changed = new Subject<FileSystemEventArgs>();
public FileWatcher( string file )
{
// Create a new FileSystemWatcher and set its properties.
_Watcher = new FileSystemWatcher
{
Path = Path.GetDirectoryName(file),
NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName,
Filter =Path.GetFileName(file)
};
// Add event handlers.
_Watcher.Changed += OnChanged;
_Watcher.Created += OnChanged;
_Watcher.Deleted += OnChanged;
_Watcher.Renamed += OnChanged;
// Begin watching.
_Watcher.EnableRaisingEvents = true;
}
// Define the event handlers.
private void OnChanged( object source, FileSystemEventArgs e )
{
Changed.OnNext(e);
}
public void Dispose()
{
_Watcher.Dispose();
}
}
}
и некоторые утилиты, использующие все вышеперечисленное.
public static class FileUtils
{
public static IObservable<FileSystemEventArgs> ChangedObservable(string path)
{
if (path == null)
return Observable.Never<FileSystemEventArgs>();
return Observable.Using(() => new FileWatcher(path), watcher => watcher.Changed);
}
public static Task DeleteDirectoryAsync(string path, bool recurse)
{
var task = new TaskCompletionSource<Unit>();
if (Directory.Exists(path))
{
ChangedObservable(path)
.Where(f => f.ChangeType == WatcherChangeTypes.Deleted)
.Take(1)
.Subscribe(v => task.SetResult(Unit.Default));
Directory.Delete(path, recurse);
}
else
{
task.SetResult(Unit.Default);
}
return task.Task;
}
}