private CachedDirectoryInfo WatchOnDirectory(string directory, string fileMask, Action onChangeAction) { lock (directoryInfo) { var cacheRecord = directoryInfo[directory]; if (cacheRecord == null) { cacheRecord = new CachedDirectoryInfo { FileSystemWatcher = new FileSystemWatcher(directory, fileMask) { IncludeSubdirectories = true, }, OnChange = onChangeAction }; cacheRecord.FileSystemWatcher.Created += (a, b) => OnFileChanged(cacheRecord); cacheRecord.FileSystemWatcher.Changed += (a, b) => OnFileChanged(cacheRecord); cacheRecord.FileSystemWatcher.Deleted += (a, b) => OnFileChanged(cacheRecord); cacheRecord.FileSystemWatcher.Renamed += (a, b) => OnFileChanged(cacheRecord); cacheRecord.FileSystemWatcher.EnableRaisingEvents = true; directoryInfo[directory] = cacheRecord; } return(cacheRecord); } }
private DateTime GetCachedFolderLastUpdateDateUtc(string directory, string fileMask) { var cacheRecord = _directoryInfo[directory]; if (cacheRecord == null) { lock (_directoryInfo) { cacheRecord = _directoryInfo[directory]; if (cacheRecord == null) { cacheRecord = new CachedDirectoryInfo(); cacheRecord.FileSystemWatcher = new FileSystemWatcher(directory, fileMask); cacheRecord.FileSystemWatcher.IncludeSubdirectories = true; cacheRecord.FileSystemWatcher.Created += (a, b) => cacheRecord.LastTimeUpdatedUtc = null; cacheRecord.FileSystemWatcher.Changed += (a, b) => cacheRecord.LastTimeUpdatedUtc = null; cacheRecord.FileSystemWatcher.Deleted += (a, b) => cacheRecord.LastTimeUpdatedUtc = null; cacheRecord.FileSystemWatcher.Renamed += (a, b) => cacheRecord.LastTimeUpdatedUtc = null; cacheRecord.FileSystemWatcher.EnableRaisingEvents = true; _directoryInfo[directory] = cacheRecord; } } } var cachedResult = cacheRecord.LastTimeUpdatedUtc; if (cachedResult.HasValue) { return(cachedResult.Value); } var files = Directory.GetFiles(directory, fileMask, SearchOption.AllDirectories); if (files.Length == 0) { return(DateTime.Now); } DateTime result = File.GetLastWriteTimeUtc(files[0]); foreach (var file in files.Skip(1)) { DateTime lastUpdatedUtc = File.GetLastWriteTimeUtc(file); if (lastUpdatedUtc > result) { result = lastUpdatedUtc; } } cacheRecord.LastTimeUpdatedUtc = result; return(result); }
private DateTime GetCachedFolderLastUpdateDateUtc(string directory, string fileMask) { var cacheRecord = _directoryInfo[directory]; if (cacheRecord == null) { lock (_directoryInfo) { cacheRecord = _directoryInfo[directory]; if (cacheRecord == null) { cacheRecord = new CachedDirectoryInfo(); cacheRecord.FileSystemWatcher = new FileSystemWatcher(directory, fileMask); cacheRecord.FileSystemWatcher.IncludeSubdirectories = true; cacheRecord.FileSystemWatcher.Created += (a, b) => cacheRecord.LastTimeUpdatedUtc = null; cacheRecord.FileSystemWatcher.Changed += (a, b) => cacheRecord.LastTimeUpdatedUtc = null; cacheRecord.FileSystemWatcher.Deleted += (a, b) => cacheRecord.LastTimeUpdatedUtc = null; cacheRecord.FileSystemWatcher.Renamed += (a, b) => cacheRecord.LastTimeUpdatedUtc = null; cacheRecord.FileSystemWatcher.EnableRaisingEvents = true; _directoryInfo[directory] = cacheRecord; } } } var cachedResult = cacheRecord.LastTimeUpdatedUtc; if (cachedResult.HasValue) return cachedResult.Value; var files = Directory.GetFiles(directory, fileMask, SearchOption.AllDirectories); if (files.Length == 0) return DateTime.Now; DateTime result = File.GetLastWriteTimeUtc(files[0]); foreach (var file in files.Skip(1)) { DateTime lastUpdatedUtc = File.GetLastWriteTimeUtc(file); if (lastUpdatedUtc > result) { result = lastUpdatedUtc; } } cacheRecord.LastTimeUpdatedUtc = result; return result; }
private void OnFileChanged(CachedDirectoryInfo cachedDirectoryInfo) { cachedDirectoryInfo.LastTimeUpdatedUtc = DateTime.Now; cachedDirectoryInfo.OnChange?.Invoke(); }