private void UpdatePie(string rootDirectory, int depth, bool showFiles) { if (!Directory.Exists(rootDirectory) && !File.Exists(rootDirectory)) { return; } var dataPoints = new FileSystemEnumerable <PieDataPoint>(rootDirectory, (ref FileSystemEntry entry) => CreateDataPoint(ref entry, depth), new EnumerationOptions() { RecurseSubdirectories = true, AttributesToSkip = 0 }) { ShouldRecursePredicate = (ref FileSystemEntry entry) => GetDepth(ref entry) < depth, ShouldIncludePredicate = (ref FileSystemEntry entry) => showFiles || entry.IsDirectory }; if (!showFiles) { var dataPoint = new PieDataPoint(GetDirectorySize(rootDirectory, recurse: depth == 0), "."); dataPoint.Label = String.Empty; this.pieSeries.DataPoints.Add(dataPoint); } foreach (var dataPoint in dataPoints) { dataPoint.Label = String.Empty; this.pieSeries.DataPoints.Add(dataPoint); } }
private void IsHiddenAttributeInternal(bool useDotPrefix, bool useHiddenFlag) { string prefix = useDotPrefix ? "." : ""; DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); FileInfo fileOne = new FileInfo(Path.Combine(testDirectory.FullName, GetTestFileName())); FileInfo fileTwo = new FileInfo(Path.Combine(testDirectory.FullName, prefix + GetTestFileName())); fileOne.Create().Dispose(); fileTwo.Create().Dispose(); if (useHiddenFlag) { fileTwo.Attributes |= FileAttributes.Hidden; } FileInfo fileCheck = new FileInfo(fileTwo.FullName); Assert.Equal(fileTwo.Attributes, fileCheck.Attributes); IEnumerable <string> enumerable = new FileSystemEnumerable <string>( testDirectory.FullName, (ref FileSystemEntry entry) => entry.ToFullPath(), new EnumerationOptions() { AttributesToSkip = 0 }) { ShouldIncludePredicate = (ref FileSystemEntry entry) => entry.IsHidden }; Assert.Equal(new string[] { fileTwo.FullName }, enumerable); }
public static async Task UploadAsync( string sourceDirectory, string destination, IProgress <StructuredReport> progress, CancellationToken cancellationToken) { var structuredProgress = progress.Start("Logging in and enumerating files"); var((client, itemRequestBuilderFactory), files) = await( GetClientAndItemRequestBuilderFactoryAsync(), Task.Run( () => { var enumerable = new FileSystemEnumerable <EnumeratedFileData>( sourceDirectory, EnumeratedFileData.FromFileSystemEntry, new EnumerationOptions { AttributesToSkip = FileAttributes.System, RecurseSubdirectories = true, IgnoreInaccessible = false, }); enumerable.ShouldIncludePredicate = ShouldInclude; return(enumerable.ToImmutableArray());
public void IsReadOnlyAttribute() { DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); FileInfo fileOne = new FileInfo(Path.Combine(testDirectory.FullName, GetTestFileName())); FileInfo fileTwo = new FileInfo(Path.Combine(testDirectory.FullName, GetTestFileName())); fileOne.Create().Dispose(); fileTwo.Create().Dispose(); fileTwo.Attributes |= FileAttributes.ReadOnly; IEnumerable <string> enumerable = new FileSystemEnumerable <string>( testDirectory.FullName, (ref FileSystemEntry entry) => entry.ToFullPath(), new EnumerationOptions() { AttributesToSkip = 0 }) { ShouldIncludePredicate = (ref FileSystemEntry entry) => (entry.Attributes & FileAttributes.ReadOnly) != 0 }; Assert.Equal(new string[] { fileTwo.FullName }, enumerable); }
public void IsHiddenAttribute() { DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); FileInfo fileOne = new FileInfo(Path.Combine(testDirectory.FullName, GetTestFileName())); // Put a period in front to make it hidden on Unix FileInfo fileTwo = new FileInfo(Path.Combine(testDirectory.FullName, "." + GetTestFileName())); fileOne.Create().Dispose(); fileTwo.Create().Dispose(); if (PlatformDetection.IsWindows) { fileTwo.Attributes = fileTwo.Attributes | FileAttributes.Hidden; } IEnumerable <string> enumerable = new FileSystemEnumerable <string>( testDirectory.FullName, (ref FileSystemEntry entry) => entry.ToFullPath(), new EnumerationOptions() { AttributesToSkip = 0 }) { ShouldIncludePredicate = (ref FileSystemEntry entry) => entry.IsHidden }; Assert.Equal(new string[] { fileTwo.FullName }, enumerable); }
public void DiscoverFiles(string directory, Action <string> action) { var enumerationOptions = new EnumerationOptions() { RecurseSubdirectories = false, AttributesToSkip = FileAttributes.Directory | FileAttributes.Device | FileAttributes.Hidden }; var files = new FileSystemEnumerable <FileInfo>(directory, (ref FileSystemEntry entry) => (FileInfo)entry.ToFileSystemInfo(), enumerationOptions) { ShouldIncludePredicate = (ref FileSystemEntry entry) => FileHelper.IsImage(entry.FileName.ToString()) }; _logger.Log($"Found {files.Count()} files"); foreach (var file in files) { _logger.Log($"Uploading {file.FullName}"); action.Invoke(file.FullName); } }
/// <summary> /// loads the files contained in the current folder /// </summary> private void LoadFolderDetailGridView() { DirectoryInfo dirInfo = new DirectoryInfo(folder); IEnumerable <LogFile> info = new FileSystemEnumerable(dirInfo, "*.log", SearchOption.AllDirectories).Cast <FileInfo>().Select(a => new LogFile { Name = a.Name, FullPath = a.FullName, Size = a.Length }); FileListGridView.ItemsSource = info.ToList <LogFile>(); }
public IEnumerator <FileSystemInfo> GetEnumerator() { if (_root == null || !_root.Exists) { yield break; } IEnumerable <FileSystemInfo> matches = new List <FileSystemInfo>(); try { //_logger.DebugFormat("Attempting to enumerate '{0}'", _root.FullName); matches = _patterns.Aggregate(matches, (current, pattern) => current.Concat(_root.EnumerateDirectories(pattern, SearchOption.TopDirectoryOnly)) .Concat(_root.EnumerateFiles(pattern, SearchOption.TopDirectoryOnly))); } catch (UnauthorizedAccessException) { //_logger.WarnFormat("Unable to access '{0}'. Skipping...", _root.FullName); yield break; } catch (PathTooLongException) { //_logger.Warn(string.Format(@"Could not process path '{0}\{1}'.", _root.Parent.FullName, _root.Name), ptle); yield break; } catch (System.IO.IOException) { // "The symbolic link cannot be followed because its type is disabled." // "The specified network name is no longer available." //_logger.Warn(string.Format(@"Could not process path (check SymlinkEvaluation rules)'{0}\{1}'.", _root.Parent.FullName, _root.Name), e); yield break; } //_logger.DebugFormat("Returning all objects that match the pattern(s) '{0}'", string.Join(",", _patterns)); foreach (var file in matches) { yield return(file); } if (_option != SearchOption.AllDirectories) { yield break; } //_logger.DebugFormat("Enumerating all child directories."); foreach (var dir in _root.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)) { //_logger.DebugFormat("Enumerating '{0}'", dir.FullName); var fileSystemInfos = new FileSystemEnumerable(dir, _patterns, _option); foreach (var match in fileSystemInfos) { yield return(match); } } }
public IEnumerator <FileSystemInfo> GetEnumerator() { if (_root == null || !_root.Exists) { yield break; } IEnumerable <FileSystemInfo> matches = new List <FileSystemInfo>(); try { _logger.DebugFormat("Attempting to enumerate '{0}'", _root.FullName); foreach (var pattern in _patterns) { _logger.DebugFormat("Using pattern '{0}'", pattern); matches = matches.Concat(_root.EnumerateDirectories(pattern, SearchOption.TopDirectoryOnly)) .Concat(_root.EnumerateFiles(pattern, SearchOption.TopDirectoryOnly)); } } catch (UnauthorizedAccessException) { _logger.WarnFormat("Unable to access '{0}'. Skipping...", _root.FullName); yield break; } catch (PathTooLongException ptle) { _logger.Warn(string.Format(@"Could not process path '{0}\{1}'.", _root.Parent.FullName, _root.Name), ptle); yield break; } _logger.DebugFormat("Returning all objects that match the pattern(s) '{0}'", string.Join(",", _patterns)); foreach (var file in matches) { yield return(file); } if (_option == SearchOption.AllDirectories) { _logger.DebugFormat("Enumerating all child directories."); foreach (var dir in _root.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)) { _logger.DebugFormat("Enumerating '{0}'", dir.FullName); var fileSystemInfos = new FileSystemEnumerable(dir, _patterns, _option); foreach (var match in fileSystemInfos) { yield return(match); } } } }
public void EnumerateFileSystemEntries_LinksWithCycles_ShouldNotThrow() { DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); IEnumerable <string> enumerable = new FileSystemEnumerable <string>( testDirectory.FullName, (ref FileSystemEntry entry) => entry.ToFullPath(), // Skipping attributes would force a disk hit which enters the cyclic symlink new EnumerationOptions() { AttributesToSkip = 0 }); Assert.Single(enumerable); }
public void GetFileNamesEnumerable() { DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); File.Create(Path.Join(testDirectory.FullName, "one")).Dispose(); File.Create(Path.Join(testDirectory.FullName, "two")).Dispose(); Directory.CreateDirectory(Path.Join(testDirectory.FullName, "three")); IEnumerable <string> fileNames = new FileSystemEnumerable <string>( testDirectory.FullName, (ref FileSystemEntry entry) => entry.FileName.ToString()) { ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory }; FSAssert.EqualWhenOrdered(new string[] { "one", "two" }, fileNames); }
public void GetFileNamesEnumerable() { // https://blogs.msdn.microsoft.com/jeremykuhne/2018/03/09/custom-directory-enumeration-in-net-core-2-1/ DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath()); File.Create(Path.Join(testDirectory.FullName, "one")).Dispose(); File.Create(Path.Join(testDirectory.FullName, "two")).Dispose(); Directory.CreateDirectory(Path.Join(testDirectory.FullName, "three")); IEnumerable <string> fileNames = new FileSystemEnumerable <string>( testDirectory.FullName, (ref FileSystemEntry entry) => entry.FileName.ToString()) { ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory }; FSAssert.EqualWhenOrdered(new string[] { "one", "two" }, fileNames); }
public IEnumerator <string> GetEnumerator() { if (_root == null || !Directory.Exists(_root)) { yield break; } IEnumerable <string> matches = new List <string>(); try { foreach (string pattern in _patterns) { matches = matches.Concat(Directory.EnumerateDirectories(_root, pattern, SearchOption.TopDirectoryOnly)) .Concat(Directory.EnumerateFiles(_root, pattern, SearchOption.TopDirectoryOnly)); } } catch (UnauthorizedAccessException) { yield break; } catch (PathTooLongException) { yield break; } foreach (var file in matches) { yield return(file); } if (_option == SearchOption.AllDirectories) { foreach (var dir in Directory.EnumerateDirectories(_root, "*", SearchOption.TopDirectoryOnly)) { var fileSystemInfos = new FileSystemEnumerable(dir, _patterns, _option); foreach (var match in fileSystemInfos) { yield return(match); } } } }
static void Main() { var enumeration = new FileSystemEnumerable <string>( directory: Path.GetTempPath(), // search Temp directory transform: (ref FileSystemEntry entry) => entry.ToFullPath(), // map FileSystemEntry to string (see FileSystemEnumerable generic argument) options: new EnumerationOptions() { RecurseSubdirectories = true }) { // The following predicate will be used to filter the file entries ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory && Path.GetExtension(entry.ToFullPath()) == ".tmp" }; // Prints all ".tmp" files from Temp directory foreach (string filePath in enumeration) { Console.WriteLine(filePath); } }
public void EnumerateFiles_LinksWithCycles_ShouldNotThrow() { DirectoryInfo testDirectory = CreateDirectoryContainingSelfReferencingSymbolicLink(); IEnumerable <string> enumerable = new FileSystemEnumerable <string>( testDirectory.FullName, (ref FileSystemEntry entry) => entry.ToFullPath(), // Skipping attributes would force a disk hit which enters the cyclic symlink new EnumerationOptions() { AttributesToSkip = 0 }) { ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory }; // Windows differentiates between dir symlinks and file symlinks int expected = OperatingSystem.IsWindows() ? 0 : 1; Assert.Equal(expected, enumerable.Count()); }
static void ScanFiles(string path) { DriveInfo drive = ScannedDrives.First(d => path.StartsWith(d.CurrentDriveLetter)); WindowsInterop.RtlSetProcessPlaceholderCompatibilityMode(2); int baseBuffer = 8192; FileSystemEnumerable <ScannedFileInfo> fse = new FileSystemEnumerable <ScannedFileInfo>(path, (ref FileSystemEntry entry) => new ScannedFileInfo() { FullPath = entry.ToFullPath(), Size = entry.Length, Drive = drive }, new EnumerationOptions() { RecurseSubdirectories = true }); var context = new WTFContext(); context.FilePaths.AddRange(fse); context.SaveChanges(); }
public DelegateEnumerator(FileSystemEnumerable <TResult> enumerable, bool isNormalized) : base(enumerable._directory, isNormalized, enumerable._options) { _enumerable = enumerable; }
public DelegateEnumerator(FileSystemEnumerable <TResult> enumerable) : base(enumerable._directory, enumerable._options) { _enumerable = enumerable; }
/// <inheritdoc /> protected override IEnumerable <FileSystemItem> EnumerateItemsImpl(UPath path, SearchOption searchOption, SearchPredicate?searchPredicate) { if (IsOnWindows) { if (IsWithinSpecialDirectory(path)) { if (!SpecialDirectoryExists(path)) { throw NewDirectoryNotFoundException(path); } // Only sub folder "/drive/" on root folder / if (path == UPath.Root) { var item = new FileSystemItem(this, PathDrivePrefixOnWindows, true); if (searchPredicate == null || searchPredicate(ref item)) { yield return(item); } if (searchOption == SearchOption.AllDirectories) { foreach (var subItem in EnumerateItemsImpl(PathDrivePrefixOnWindows, searchOption, searchPredicate)) { yield return(subItem); } } yield break; } // When listing for /drive, return the list of drives available if (path == PathDrivePrefixOnWindows) { var pathDrives = new List <UPath>(); foreach (var drive in DriveInfo.GetDrives()) { if (drive.Name.Length < 2 || drive.Name[1] != ':') { continue; } var pathDrive = PathDrivePrefixOnWindows / char.ToLowerInvariant(drive.Name[0]).ToString(); pathDrives.Add(pathDrive); var item = new FileSystemItem(this, pathDrive, true); if (searchPredicate == null || searchPredicate(ref item)) { yield return(item); } } if (searchOption == SearchOption.AllDirectories) { foreach (var pathDrive in pathDrives) { foreach (var subItem in EnumerateItemsImpl(pathDrive, searchOption, searchPredicate)) { yield return(subItem); } } } yield break; } } } var pathOnDisk = ConvertPathToInternal(path); if (!Directory.Exists(pathOnDisk)) { yield break; } #if NETSTANDARD2_1 var enumerable = new FileSystemEnumerable <FileSystemItem>(pathOnDisk, TransformToFileSystemItem, searchOption == SearchOption.AllDirectories ? CompatibleRecursive : Compatible); foreach (var item in enumerable) { var localItem = item; if (searchPredicate == null || searchPredicate(ref localItem)) { yield return(localItem); } } #else var results = Directory.EnumerateFileSystemEntries(pathOnDisk, "*", searchOption); foreach (var subPath in results) { var fileInfo = new FileInfo(subPath); var fullPath = ConvertPathFromInternal(subPath); var item = new FileSystemItem { FileSystem = this, AbsolutePath = fullPath, Path = fullPath, Attributes = fileInfo.Attributes, CreationTime = fileInfo.CreationTimeUtc.ToLocalTime(), LastAccessTime = fileInfo.LastAccessTimeUtc.ToLocalTime(), LastWriteTime = fileInfo.LastWriteTimeUtc.ToLocalTime(), Length = (fileInfo.Attributes & FileAttributes.Directory) > 0 ? 0 : fileInfo.Length }; if (searchPredicate == null || searchPredicate(ref item)) { yield return(item); } } #endif }