private void EnsureInitialized() { try { _entries = new DirectoryInfo(_directory) .EnumerateFileSystemInfos() .Where(info => !FileSystemInfoHelper.IsExcluded(info, _filters)) .Select <FileSystemInfo, IFileInfo>(info => { if (info is FileInfo file) { return(new PhysicalFileInfo(file)); } else if (info is DirectoryInfo dir) { return(new PhysicalDirectoryInfo(dir)); } // shouldn't happen unless BCL introduces new implementation of base type throw new InvalidOperationException("Unexpected type of FileSystemInfo"); }); } catch (Exception ex) when(ex is DirectoryNotFoundException || ex is IOException) { _entries = Enumerable.Empty <IFileInfo>(); } }
private void OnFileSystemEntryChange(string fullPath) { try { var fileSystemInfo = new FileInfo(fullPath); if (FileSystemInfoHelper.IsExcluded(fileSystemInfo, _filters)) { return; } var relativePath = fullPath.Substring(_root.Length); ReportChangeForMatchedEntries(relativePath); } catch (Exception ex) when( ex is IOException || ex is SecurityException || ex is UnauthorizedAccessException) { // Swallow the exception. } }
/// <summary> /// Locate a file at the given path by directly mapping path segments to physical directories. /// </summary> /// <param name="subpath">A path under the root directory</param> /// <returns>The file information. Caller must check <see cref="IFileInfo.Exists"/> property. </returns> public IFileInfo GetFileInfo(string subpath) { subpath = AdjustSubpath(subpath); if (string.IsNullOrEmpty(subpath) || PathUtils.HasInvalidPathChars(subpath)) { return(new NotFoundFileInfo(subpath)); } // Relative paths starting with leading slashes are okay subpath = subpath.TrimStart(PathSeparators); // Absolute paths not permitted. if (Path.IsPathRooted(subpath)) { return(new NotFoundFileInfo(subpath)); } var fullPath = GetFullPath(subpath); if (fullPath == null) { return(new NotFoundFileInfo(subpath)); } var fileInfo = new FileInfo(fullPath); if (FileSystemInfoHelper.IsExcluded(fileInfo, _filters)) { return(new NotFoundFileInfo(subpath)); } var info = new PhysicalFileInfo(fileInfo); return(info); }