/// <summary> /// Creates a filewatcher for the FileEnding of the watched file /// </summary> /// <param name="w">the file to watch for</param> private void createNewFileWatcher(watchedFile w) { if (w.path == null || w.path == string.Empty) { return; } string fileName = w.path.Remove(0, w.path.LastIndexOf(@"\") + 1); filesToWatch_List.Add(w); // neuen fileWatcher für Dateiendung erstellen FileSystemWatcher fsw = new FileSystemWatcher(); fsw.Filter = "*" + new FileInfo(w.path).Extension; foreach (FileSystemWatcher watcher in watchers) { //gibt es für die Endung schon eine Überwachung? if (watcher.Filter == fsw.Filter) { return; } } fsw.IncludeSubdirectories = false; fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; fsw.Path = @"C:\"; fsw.IncludeSubdirectories = true; fsw.Created += new FileSystemEventHandler(OnFileChanged); fsw.Deleted += new FileSystemEventHandler(OnFileChanged); fsw.Renamed += new RenamedEventHandler(OnFileRenamed); fsw.EnableRaisingEvents = true; watchers.Add(fsw); }
/// <summary> /// Used when file that is watched is renamed /// </summary> /// <param name="source"></param> /// <param name="e"></param> private void OnFileRenamed(object source, RenamedEventArgs e) { string oldFullPath = e.OldFullPath; string oldFile = e.OldFullPath.Remove(0, e.OldFullPath.LastIndexOf(@"\") + 1); string fullPath = e.FullPath; string file = e.FullPath.Remove(0, e.FullPath.LastIndexOf(@"\") + 1); // ist die umbenannte datei in Überwachungsliste? bool exists = false; for (int i = 0; i < filesToWatch_List.Count; i++) { if (filesToWatch_List[i].path.EndsWith(file) || filesToWatch_List[i].path.EndsWith(oldFile)) { exists = true; break; } } // wenn ja, dann dateinamen aktualisieren if (exists) { MessageBox.Show(string.Format("File: {0} renamed to {1}", oldFullPath, fullPath)); List <int> indizes = new List <int>(); for (int i = 0; i < filesToWatch_List.Count; i++) { // entsprechenden index in der übwachungsliste raussuchen if (filesToWatch_List[i].path == oldFullPath || filesToWatch_List[i].path.EndsWith(oldFile)) { indizes.Add(i); } } if (indizes.Count > 1) { MessageBox.Show("mist, die datei ist zweimal in der Überwachungsliste"); } if (indizes.Count == 1) { // daten der überwachungsdatei aktualisieren watchedFile w = new watchedFile(filesToWatch_List[indizes[0]].name, fullPath, filesToWatch_List[indizes[0]].tags); filesToWatch_List.RemoveAt(indizes[0]); filesToWatch_List.Add(w); } } }
// File Handling /// <summary> /// Used when file that is watched in added or deleted /// </summary> /// <param name="source"></param> /// <param name="e"></param> private void OnFileChanged(object source, FileSystemEventArgs e) { string fullPath = e.FullPath; watchedFile w = filesToWatch_List.Find(x => x.path == fullPath); string file = e.FullPath.Remove(0, e.FullPath.LastIndexOf(@"\") + 1); // Datei wurde gelöscht, entferne aus Überwachungsliste, füge zu gelöscht liste hinzu if (e.ChangeType == WatcherChangeTypes.Deleted) { filesDeleted.Add(w); int a = filesToWatch_List.FindIndex(x => x.path == fullPath); if (a >= 0) { filesToWatch_List.RemoveAt(a); } } // Datei wurde erstellt if (e.ChangeType == WatcherChangeTypes.Created) { // Überprüfe ob hinzugefügte Datei in gelöscht-Liste List <int> indizes = new List <int>(); for (int i = 0; i < filesDeleted.Count; i++) { if (filesDeleted[i].path.EndsWith(file)) { indizes.Add(i); } } if (indizes.Count > 1) { MessageBox.Show("mist, die datei existiert mehrfach in der überwachungsliste"); } // Wenn ja ist die hinzugefügte Datei nur verschoben worden -> lösche aus gelöscht, füge mit neuem pfad zu überwachung hinzu if (indizes.Count == 1) { filesDeleted.RemoveAt(indizes[0]); filesToWatch_List.Add(w); } } }
/// <summary> /// creates a list of files to be watched from the files.elo file /// </summary> private void InitializeFiles() { filesToWatch_List.Clear(); if (!File.Exists(@"..\files.elo")) { return; } foreach (string s in File.ReadAllLines(@"..\files.elo")) { string[] parts = s.Split(';'); List <string> tags = new List <string>(parts[2].Split(',')); watchedFile w = new watchedFile(parts[0], parts[1], tags); filesToWatch_List.Add(w); foreach (string tag in tags) { if (!existingTags.Contains(tag)) { existingTags.Add(tag); } } } }