public async Task StartWatcher(int watcherID) { bool isTripped = false; Watcher watcher = await this.watcherRepo.FirstOrDefaultAsync(a => a.ID == watcherID); SymbolicLink symbolicLink = await this.symbolicLinkRepo.FirstOrDefaultAsync(a => a.ID == watcher.SymbolicLinkID); System.Tuple <int, int> monitorKey = new System.Tuple <int, int>(watcher.SymbolicLinkID, watcher.ID); if (DirWatcherTransferApp.Monitors.ContainsKey(monitorKey)) { throw new System.Exception("Watcher already has monitor running. Please end the existing monitor before starting a new one."); } FileSystemMonitor fileSystemMonitor = new FileSystemMonitor(); // This actions gets fired when a file changes. fileSystemMonitor.CopyCompletedAction = async(notifyFilter, fileSystemEventArgs) => { // TODO: Implement a better way to check for visual studio temp files. This will fail if the actual file have a "~" in the name. if (!isTripped && fileSystemEventArgs.Name.Contains("~") && !fileSystemEventArgs.Name.EndsWith("~")) { isTripped = true; string fileName = fileSystemEventArgs.Name; string filePath = fileSystemEventArgs.FullPath; if (fileSystemEventArgs.Name.Contains("~") && !fileSystemEventArgs.Name.EndsWith("~")) { // Remove the vs caching name format. fileName = fileSystemEventArgs.Name.Remove(fileSystemEventArgs.Name.LastIndexOf("~")); filePath = fileSystemEventArgs.FullPath.Replace(fileSystemEventArgs.Name, fileName); } // Sync the changed file with the target file and update the SignalR clients of the copied file. CopyDiagnostics copyDiagnostics = await this.SyncLinkedFile(fileName, filePath); await this.fileSystemHubContext.Clients.All.SendAsync("onFileCopied", copyDiagnostics); await LogUtility.WriteToLog(notifyFilter, copyDiagnostics); // Update counts. if (notifyFilter != null) { await this.symbolicLinkRepo.IncrementCount(fileSystemEventArgs.FullPath.Replace(@"\" + fileSystemEventArgs.Name, string.Empty), (NotifyFilters)notifyFilter); await this.watcherRepo.IncrementCount(watcher, (NotifyFilters)notifyFilter); } isTripped = false; } }; fileSystemMonitor.StartWatcher(symbolicLink.Source, DirWatcherTransferApp.ProcessWatcherFiltersAsList(watcher)); DirWatcherTransferApp.Monitors.Add(monitorKey, fileSystemMonitor); }