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>(); } }
/// <summary> /// Locate a directory 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 directory information. Caller must check /// <see cref="IFileInfo.Exists"/> property. /// </returns> public IFileInfo GetDirectoryInfo(string 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 = this.GetFullPath(subpath); if (fullPath == null) { return(new NotFoundFileInfo(subpath)); } var directoryInfo = new DirectoryInfo(fullPath); if (FileSystemInfoHelper.IsExcluded(directoryInfo, this._filters)) { return(new NotFoundFileInfo(subpath)); } return(new PhysicalDirectoryInfo(directoryInfo)); }
public void FiltersExcludedDirectories(string dirname, FileAttributes attributes, ExclusionFilters filters, bool excluded) { var dirInfo = new DirectoryInfo(Path.Combine(_fileSystem.RootPath, dirname)); dirInfo.Create(); dirInfo.Attributes = attributes; Assert.Equal(excluded, FileSystemInfoHelper.IsExcluded(_fileSystem.GetDirectory(dirname), filters)); }
public void FiltersExcludedFiles(string filename, FileAttributes attributes, ExclusionFilters filters, bool excluded) { var fileInfo = new FileInfo(Path.Combine(_fileSystem.RootPath, filename)); _fileSystem.CreateFile(fileInfo); fileInfo.Attributes = attributes; Assert.Equal(excluded, FileSystemInfoHelper.IsExcluded(_fileSystem.GetFile(filename), filters)); }
public void FiltersExcludedFiles(string filename, FileAttributes attributes, ExclusionFilters filters, bool excluded) { using var fileSystem = new TempDirectory(GetTestFilePath()); var fileInfo = new FileInfo(Path.Combine(fileSystem.Path, filename)); using (var stream = fileInfo.Create()) using (var writer = new StreamWriter(stream)) { writer.Write("temp"); } fileInfo.Attributes = attributes; Assert.Equal(excluded, FileSystemInfoHelper.IsExcluded(fileInfo, filters)); }
private void OnFileSystemEntryChange(string fullPath) { try { var fileSystemInfo = new FileInfo(fullPath); if (FileSystemInfoHelper.IsHiddenFile(fileSystemInfo)) { 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. } }