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)); } } } }
private void CompareStaticFSData(StaticFSData newData, StaticFSData oldData) { int raiseEventCount = 0; //only flag for the event to be raised through the notify loops below so that 8 changed events won't come up. if ((NotifyFilter & NotifyFilters.LastWrite) == NotifyFilters.LastWrite) { if (newData.LastWriteTime != oldData.LastWriteTime) { raiseEventCount++; } } if ((NotifyFilter & NotifyFilters.Attributes) == NotifyFilters.Attributes) { if (newData.Attributes != oldData.Attributes) { raiseEventCount++; } } if ((NotifyFilter & NotifyFilters.CreationTime) == NotifyFilters.CreationTime) { if (newData.CreationTime != oldData.CreationTime) { raiseEventCount++; } } if ((NotifyFilter & NotifyFilters.FileName) == NotifyFilters.FileName) //This shouldn't happen because filename changes will flag delete or creation. { if (newData.Name != oldData.Name) { raiseEventCount++; } } if (newData is StaticDirectoryData == false) //Don't check lastAccess for directories. This app was updating lastAccess. { if ((NotifyFilter & NotifyFilters.LastAccess) == NotifyFilters.LastAccess) { if (newData.LastAccessTime != oldData.LastAccessTime) { raiseEventCount++; } } } if ((NotifyFilter & NotifyFilters.Size) == NotifyFilters.Size) //Size only applies to files. Size will be 0 for directories { if (newData.Size != oldData.Size) { raiseEventCount++; } } if (RaiseAllEvents == true) { for (int n = 0; n < raiseEventCount; n++) { //raise an event for each change registered. if (Changed != null) { Changed(this, new FileSystemEventArgs(WatcherChangeTypes.Changed, newData.ParentDirectory, newData.Name)); } } } else { //then only raise one event per file no matter how many changes registered if (raiseEventCount > 0) { if (Changed != null) { Changed(this, new FileSystemEventArgs(WatcherChangeTypes.Changed, newData.FullName, newData.Name)); } } } }