public void RegisterTest() { bool? bChanged = false; string fileName = Path.Combine(Directory.GetCurrentDirectory(), "test1.txt"); string fileName0 = Path.Combine(Directory.GetCurrentDirectory(), "test2.txt"); using (var watcher = new SingleFileWatcher(fileName)) { Assert.AreEqual(false, bChanged); Action notify = () => bChanged = true; File.WriteAllText(fileName, "کپها رو میگیره"); watcher.Register(notify); File.WriteAllText(fileName, "کپها رو میگیرن"); Thread.Sleep(10); Assert.AreEqual(true, bChanged); bChanged = false; File.WriteAllText(fileName0, "do not get notified"); Thread.Sleep(10); Assert.AreEqual(false, bChanged); } File.Delete(fileName); File.Delete(fileName0); }
public void Add(string filePath) { if (fileWatchers.ContainsKey(filePath)) { return; } FileInfo file = new FileInfo(filePath); if (!file.Directory.Exists) { return; } SingleFileWatcher fw = new SingleFileWatcher(file); fw.Watcher.Path = file.DirectoryName; fw.Watcher.Filter = file.Name; fw.Watcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite; fw.Watcher.Changed += OnChanged; fw.Watcher.EnableRaisingEvents = false; fileWatchers.Add(filePath, fw); if (fileWatchers.Count > 0) { Start(); } }
private void EnableWatchers(bool enable) { foreach (string key in fileWatchers.Keys) { SingleFileWatcher watcher = fileWatchers[key]; watcher.Watcher.EnableRaisingEvents = enable; } }
public void Clear() { Stop(); foreach (string key in fileWatchers.Keys) { SingleFileWatcher fw = fileWatchers[key]; fw.Watcher.Changed -= OnChanged; } fileWatchers.Clear(); }
public SingleFileWatcherTests() { tempDirectory = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}"); watcherTempDirectory = Path.Combine(tempDirectory, $"{Guid.NewGuid():N}"); filePath = Path.Combine(watcherTempDirectory, $"{Guid.NewGuid():N}"); Directory.CreateDirectory(watcherTempDirectory); File.WriteAllText(filePath, "Hello"); dispatcher = new FileWatcherDispatcher(); singleFileWatcher = new SingleFileWatcher(filePath, dispatcher); }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { // general OnExit = new DelegateCommand(_ => { Application.Current.Shutdown(); }); // init LogView logView = ServiceLocator.Current.GetInstance <LogView>(); LogViewDataVM = new LogViewDataVM(logView); SettingsVM = new SettingsVM(logView); // menu OnLoadFile = new DelegateCommand(_ => { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == true) { string path = openFileDialog.FileName; if (this.loader != null) { this.loader.disableWatcher(); } logView.clear(); loader = new SingleFileWatcher(path, new MySimpleFileReader(logView), true); } }); OnWatchFile = new DelegateCommand(_ => { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == true) { string path = openFileDialog.FileName; if (this.loader != null) { this.loader.disableWatcher(); } logView.clear(); loader = new SingleFileWatcher(path, new MySimpleFileReader(logView), true); } }); // toggle settings ToggleSettings = new DelegateCommand(_ => { IsSettingsVisible = !IsSettingsVisible; setToggleButtonDesc(IsSettingsVisible); }); setToggleButtonDesc(IsSettingsVisible); }
public void MultiRegisterUnregister() { bool? bChange1 = false; bool? bChange2 = false; string fileName = Path.Combine(Directory.GetCurrentDirectory(), "test3.txt"); string fileName0 = Path.Combine(Directory.GetCurrentDirectory(), "test4.txt"); using (var watcher = new SingleFileWatcher(fileName)) { Assert.AreEqual(false, bChange1); Assert.AreEqual(false, bChange2); Action notify1 = () => bChange1 = true; Action notify2 = () => bChange2 = true; File.WriteAllText(fileName, "کپها رو میگیره"); watcher.Register(notify1); watcher.Register(notify2); File.WriteAllText(fileName, "کپها رو میگیرن"); Thread.Sleep(10); Assert.AreEqual(true, bChange1); Assert.AreEqual(true, bChange2); bChange1 = false; bChange2 = false; watcher.Unregister(notify1); File.WriteAllText(fileName, ":)))"); Thread.Sleep(10); Assert.AreEqual(false, bChange1); Assert.AreEqual(true, bChange2); } File.Delete(fileName); File.Delete(fileName0); }
public void Add(string filePath) { if (fileWatchers.ContainsKey(filePath)) return; FileInfo file = new FileInfo(filePath); if (!file.Directory.Exists) return; SingleFileWatcher fw = new SingleFileWatcher(file); fw.Watcher.Path = file.DirectoryName; fw.Watcher.Filter = file.Name; fw.Watcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite; fw.Watcher.Changed += OnChanged; fw.Watcher.EnableRaisingEvents = false; fileWatchers.Add(filePath, fw); if (fileWatchers.Count > 0) Start(); }
public void DisposeTest() { IDisposable sfw = new SingleFileWatcher(Directory.GetCurrentDirectory()); sfw.Dispose(); }