private void SetupWatcher() { var fullPath = CurrentWorkingDirectory == null ? null : PathUtils.GetFullPath(CurrentWorkingDirectory); if (fullPath == null || watcher == null || !string.Equals(watcher.Path, fullPath)) { watcher?.Dispose(); if (fullPath != null && Directory.Exists(fullPath)) { watcher = new FileSystemWatcher(fullPath); watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watcher.Filter = "*.*"; watcher.Changed += File_Changed; watcher.Created += File_Changed; watcher.Deleted += File_Changed; watcher.Renamed += File_Changed; lstFiles.Items.Clear(); foreach (var item in new DirectoryInfo(fullPath) .GetFiles("*.*", SearchOption.AllDirectories) .Select(x => CertificateFile.FromFile(fullPath, x.FullName))) { AddItem(item); } UpdateCaList(); UpdateCsrToSignList(); watcher.EnableRaisingEvents = true; } } }
private void File_Changed(object sender, FileSystemEventArgs e) { Invoke(new Action(() => { bool mutated = false; if ((e.ChangeType & WatcherChangeTypes.Created) != 0) { if (File.Exists(e.FullPath)) { AddItem(CertificateFile.FromFile(PathUtils.GetFullPath(CurrentWorkingDirectory), e.FullPath)); mutated = true; } } if ((e.ChangeType & WatcherChangeTypes.Deleted) != 0) { var files = lstFiles.Items.Cast <CertificateFile>() .Where(x => string.Equals(x.FullPath, e.FullPath, StringComparison.OrdinalIgnoreCase)) .ToArray(); foreach (var f in files) { RemoveItem(f); mutated = true; } } if ((e.ChangeType & WatcherChangeTypes.Renamed) != 0) { // huh? } if ((e.ChangeType & WatcherChangeTypes.Changed) != 0) { var files = lstFiles.Items.Cast <CertificateFile>() .Where(x => string.Equals(x.FullPath, e.FullPath, StringComparison.OrdinalIgnoreCase)) .ToArray(); foreach (var f in files) { ReloadItem(f); mutated = true; } } if (mutated) { UpdateCaList(); UpdateCsrToSignList(); } })); }