当我在序列old =>中更改文件的扩展名时new =>旧的,第三次我没有得到通知,即使我将扩展名改为完全不同的东西。我必须更改文件名以使其更进一步。
有什么想法吗?
我的观察者包装:
public class WatcherWrapper : IDisposable
{
private readonly FileSystemWatcher _fileWatcher;
private readonly Subject<FileSystemEventArgs> _infoSubject;
private Subject<FileSystemEventArgs> _eventSubject;
public WatcherWrapper(string path, string nameFilter = "*.*", NotifyFilters? notifyFilters = null)
{
_fileWatcher = new FileSystemWatcher(path, nameFilter);
if (notifyFilters != null)
{
_fileWatcher.NotifyFilter = notifyFilters.Value;
}
_infoSubject = new Subject<FileSystemEventArgs>();
_eventSubject = new Subject<FileSystemEventArgs>();
Observable.FromEventPattern<FileSystemEventArgs>(_fileWatcher, "Changed").Select(e => e.EventArgs)
.Distinct(e => e.FullPath).Subscribe(_infoSubject.OnNext);
Observable.FromEventPattern<FileSystemEventArgs>(_fileWatcher, "Created").Select(e => e.EventArgs)
.Distinct(e => e.FullPath).Subscribe(_infoSubject.OnNext);
Observable.FromEventPattern<FileSystemEventArgs>(_fileWatcher, "Deleted").Select(e => e.EventArgs)
.Distinct(e => e.FullPath).Subscribe(_infoSubject.OnNext);
Observable.FromEventPattern<FileSystemEventArgs>(_fileWatcher, "Renamed").Select(e => e.EventArgs)
.Distinct(e => e.FullPath).Subscribe(_infoSubject.OnNext);
_infoSubject.Buffer(TimeSpan.FromMilliseconds(100))
.Select(x =>
{
return x.GroupBy(z => z.Name).Select(z => z.LastOrDefault());
}).Subscribe(
infos =>
{
if (infos != null)
foreach (var info in infos)
{
{
_eventSubject.OnNext(info);
}
}
});
_fileWatcher.EnableRaisingEvents = true;
}
public IObservable<FileSystemEventArgs> FileEvents => _eventSubject;
public async Task<bool> WaitForFile(string path, TimeSpan timeout)
{
var end = DateTime.UtcNow + timeout;
try
{
while (DateTime.UtcNow <= end)
{
try
{
using (File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
return true;
}
}
catch (IOException)
{
await Task.Delay(200);
}
}
return false;
}
catch
{
return false;
}
}
public void Dispose()
{
_fileWatcher?.Dispose();
_eventSubject.Dispose();
_infoSubject.Dispose();
}
}
我在缓冲区(Count&gt; 0)之后的行中放置了条件断点,但没有任何东西到达那里。如果我更改了文件的名称,它会立即触发。