//set is probably called to often, when path, filter, notify filters and timer interval are set. So likely 4 times in initialization. Had it in the EnableRaiseevents set to true, but that was not really compliant with Mono/.NET way. private void set() { //sets up the FileSystemWatcher and timer. //This Function handles the comparing of the file System. if (path == "") { //throw new Exception("Path Not Set in FileSystemWatcher!"); return; } else { DirectoryInfo pathDir = new DirectoryInfo(Path); if (pathDir.Exists == false) { // throw new Exception("Directory at Path does not Exist!"); return; } else { //then we have a valid path. directoryData = new StaticDirectoryData(pathDir, IncludeSubdirectories, Filter); //now Directory Data is set, so set the timer. From here it will be good to go. tmrSample.Interval = TimerInterval; tmrSample.Enabled = true; } } }
private void tmrSample_Tick(object sender, EventArgs e) { if (EnableRaisingEvents == false) { tmrSample.Enabled = false; return; } StaticDirectoryData newDirectoryData = new StaticDirectoryData(new DirectoryInfo(Path), IncludeSubdirectories, Filter); //We now have newDirectoryData and directoryData. This is where we compare for changes. //NotifyFilter will be used to decide what to compare CompareStaticDirectoryData(newDirectoryData, directoryData); directoryData = newDirectoryData; }
private void CompareStaticDirectoryData(StaticDirectoryData newData, StaticDirectoryData oldData) { /* * This fucntion loops through the new data and for each item, finds the matching filename or foldername in the old data. * Once the match is found, compare . Raise Changed Event if needed Then Delete from old Data List * If the match is not found, then raise Created * Once done, check old list. If any are left, then raise Deleted for those items. * * All this should be done comparing againast NotifyFilter */ //First Loop through Files in newData. for (int n = 0; n < newData.Files.Count; n++) { //First check if old file exists. StaticFSData oldFile = oldData.GetSubFile(newData.Files[n].Name); if (oldFile != null) { //file was found in the old structure and therefore has not been recently added. //Now check the NotifyFilters to see what might have changed. //Calls the compare FSData Function which compares the actual LastWrite, Attributes, etc and raises and necessary events. CompareStaticFSData(newData.Files[n], oldFile); oldData.Files.Remove(oldFile); //This removes the old file so that at the end, all files left in oldData were not matched and therefore have been deleted. } else { //file was not found in old structure and therefore has been recently added. //Raise onCreated Event; if (Created != null) { Created(this, new FileSystemEventArgs(WatcherChangeTypes.Created, newData.FullName, newData.Files[n].Name)); } } }//end for loop looping through files. //Now we have looped through all the new files for (int n = 0; n < oldData.Files.Count; n++) { //Any files in this loop have been deleted so raise onDeleted if (Deleted != null) { Deleted(this, new FileSystemEventArgs(WatcherChangeTypes.Deleted, oldData.FullName, oldData.Files[n].Name)); } } //Now loop through Folders in NewData for (int n = 0; n < newData.Directories.Count; n++) { //Check if old Folder Exists. StaticDirectoryData oldFolder = oldData.GetSubDirectory(newData.Directories[n].Name); if (oldFolder != null) { //then folder existed in old structure and has not been recently added. //Calls the compare FSData Function which compares the actual LastWrite, Attributes, etc and raises and necessary events. CompareStaticFSData(newData.Directories[n], oldFolder); //now call this same function for the newFolder and the oldFolder CompareStaticDirectoryData(newData.Directories[n], oldFolder); oldData.Directories.Remove(oldFolder); //This removes the old file so that at the end, all files left in oldData were not matched and therefore have been deleted. } else { //then folder did not exist in old structure and has been recently added if (Created != null) { Created(this, new FileSystemEventArgs(WatcherChangeTypes.Created, newData.FullName, newData.Directories[n].Name)); } } } }