private static bool RefreshFilesInFolder(CachedFolderInfo folderInfo, IDirectoryCacheProvider provider, bool raiseCacheChanged) { Dictionary <string, CachedFileInfo> files = folderInfo.Files; Dictionary <string, CachedFileInfo> oldFiles = null; if (!raiseCacheChanged) { oldFiles = new Dictionary <string, CachedFileInfo>(); foreach (var oldFile in files) { oldFiles.Add(oldFile.Key, oldFile.Value); } } files.Clear(); var directoryEntries = provider.GetFilesInFolder(folderInfo.FullPath).ToList(); foreach (var file in directoryEntries) { var fileInfo = new CachedFileInfo(file.Name, file.LastWriteTimeUtc); files.Add(fileInfo.NormalisedName, fileInfo); if (oldFiles != null) { CachedFileInfo oldFile; if (!oldFiles.TryGetValue(fileInfo.NormalisedName, out oldFile)) { raiseCacheChanged = true; } else { if (oldFile.LastWriteTimeUtc != fileInfo.LastWriteTimeUtc) { raiseCacheChanged = true; } oldFiles.Remove(fileInfo.NormalisedName); } } } if (!raiseCacheChanged && oldFiles != null && oldFiles.Count != 0) { raiseCacheChanged = true; } return(raiseCacheChanged); }
private static bool RefreshSubFolders(Dictionary <string, CachedFolderInfo> cache, CachedFolderInfo folderInfo, IDirectoryCacheProvider provider, bool raiseCacheChanged, bool cacheSubFolders) { var cachedSubFolders = folderInfo.SubFolders; var subFolders = new List <CachedFolderInfo>(); var deletedSubFolders = new Dictionary <string, string>(); foreach (var name in cachedSubFolders.Select(r => r.NormalisedFullPath)) { deletedSubFolders.Add(name, name); } var subFolderNames = provider.GetSubFoldersInFolder(folderInfo.FullPath).ToList(); foreach (var subFolderName in subFolderNames) { var fullPath = Path.Combine(folderInfo.FullPath, subFolderName); var normalisedSubFolder = NormaliseFolder(fullPath); if (deletedSubFolders.ContainsKey(normalisedSubFolder)) { deletedSubFolders.Remove(normalisedSubFolder); } raiseCacheChanged = RefreshFolderCache(cache, fullPath, provider, raiseCacheChanged, cacheSubFolders); var subFolderInfo = cache[normalisedSubFolder]; subFolders.Add(subFolderInfo); } foreach (var deletedSubFolder in deletedSubFolders.Keys) { var deletedEntry = cache[deletedSubFolder]; cache.Remove(deletedSubFolder); if (deletedEntry.Files.Count > 0) { raiseCacheChanged = true; } } folderInfo.SubFolders.Clear(); folderInfo.SubFolders.AddRange(subFolders); return(raiseCacheChanged); }
private static bool RefreshFolderCache(Dictionary <string, CachedFolderInfo> cache, string folder, IDirectoryCacheProvider provider, bool raiseCacheChanged, bool cacheSubFolders) { var normalisedFolder = NormaliseFolder(folder); CachedFolderInfo folderInfo; if (!cache.TryGetValue(normalisedFolder, out folderInfo)) { folderInfo = new CachedFolderInfo(folder); cache.Add(normalisedFolder, folderInfo); } raiseCacheChanged = RefreshFilesInFolder(folderInfo, provider, raiseCacheChanged); if (cacheSubFolders) { raiseCacheChanged = RefreshSubFolders(cache, folderInfo, provider, raiseCacheChanged, cacheSubFolders); } return(raiseCacheChanged); }