/// <summary> /// Deletes the given directory. On request all contents, too. /// </summary> /// <param name="uncDirectoryPath">Path of directory to delete</param> /// <param name="recursive">If <paramref name="recursive"/> is true then all subfolders are also deleted.</param> /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception> /// <exception cref="DirectoryNotEmptyException">The directory is not empty.</exception> /// <remarks>Function loads every file and attribute. Alls read-only flags will be removed before removing.</remarks> public static void DeleteDirectory(string uncDirectoryPath, bool recursive = false) { Contract.Requires(!String.IsNullOrEmpty(uncDirectoryPath)); // Contents if (recursive) { foreach (Win32FileSystemEntry systemEntry in new Win32FileHandleCollection(QuickIOPath.Combine(uncDirectoryPath, QuickIOPatterns.PathMatchAll))) { // Create hit for current search result var resultPath = QuickIOPath.Combine(uncDirectoryPath, systemEntry.Name); if (systemEntry.IsFile) { DeleteFile(resultPath); } else if (/*is directory here*/ recursive) { DeleteDirectory(resultPath, recursive); } } } // Remove specified if (!Win32SafeNativeMethods.RemoveDirectory(uncDirectoryPath)) { int errorCode = Marshal.GetLastWin32Error(); Win32ErrorCodes.NativeExceptionMapping(uncDirectoryPath, errorCode); } }
/// <summary> /// Determined metadata of directory /// </summary> /// <param name="path">Path of the directory</param> /// <param name="fileSystemEntry"><see cref="Win32FileSystemEntry"/></param> /// <param name="enumerateOptions">The enumeration options for exception handling</param> /// <returns><see cref="QuickIODirectoryMetadata"/> started with the given directory</returns> /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception> private static QuickIODirectoryMetadata EnumerateDirectoryMetadata(String path, Win32FileSystemEntry fileSystemEntry, QuickIOEnumerateOptions enumerateOptions) { Contract.Requires(!String.IsNullOrWhiteSpace(path)); Contract.Ensures(Contract.Result <QuickIODirectoryMetadata>() != null); // Results IList <QuickIOFileMetadata> subFiles = new List <QuickIOFileMetadata>(); IList <QuickIODirectoryMetadata> subDirs = new List <QuickIODirectoryMetadata>(); foreach (Win32FileSystemEntry systemEntry in new Win32FileHandleCollection(QuickIOPath.Combine(path, QuickIOPatterns.PathMatchAll))) { // Create hit for current search result var uncResultPath = QuickIOPath.Combine(path, systemEntry.Name); // if it's a file, add to the collection if (systemEntry.IsFile) { subFiles.Add(new QuickIOFileMetadata(uncResultPath, systemEntry.FindData)); } else { subDirs.Add(EnumerateDirectoryMetadata(uncResultPath, systemEntry, enumerateOptions)); } } return(new QuickIODirectoryMetadata(path, fileSystemEntry.FindData, subDirs, subFiles)); }
/// <summary> /// Determined all sub system entries of a directory /// </summary> /// <param name="path">Path of the directory</param> /// <param name="pattern">Search pattern. Uses Win32 native filtering.</param> /// <param name="searchOption"><see cref="SearchOption"/></param> /// <param name="enumerateOptions">The enumeration options for exception handling</param> /// <returns>Collection of <see cref="QuickIODirectoryInfo"/></returns> /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception> internal static IEnumerable <Tuple <string, Win32FileSystemEntry> > EnumerateWin32FileSystemEntries(String path, String pattern = QuickIOPatterns.PathMatchAll, SearchOption searchOption = SearchOption.TopDirectoryOnly, QuickIOEnumerateOptions enumerateOptions = QuickIOEnumerateOptions.None) { Contract.Requires(!String.IsNullOrWhiteSpace(path)); Contract.Ensures(Contract.Result <IEnumerable <Tuple <string, Win32FileSystemEntry> > >() != null); // Stack Stack <string> directoryPathStack = new Stack <string>(); directoryPathStack.Push(path); while (directoryPathStack.Count > 0) { string currentDirectory = directoryPathStack.Pop(); foreach (Win32FileSystemEntry systemEntry in new Win32FileHandleCollection(QuickIOPath.Combine(currentDirectory, pattern))) { yield return(new Tuple <string, Win32FileSystemEntry>(currentDirectory, systemEntry)); // Create hit for current search result string resultPath = QuickIOPath.Combine(currentDirectory, systemEntry.Name); // Check for Directory if (searchOption == SearchOption.AllDirectories && systemEntry.IsDirectory) { directoryPathStack.Push(resultPath); } } } }
public void Combine(string p1, string p2, string expected) { string result = QuickIOPath.Combine(p1, p2); result.Should().Be(System.IO.Path.Combine(p1, p2)); result.Should().Be(expected); }
public void Combine_ArgumentNullException() { Action actionNullParameter = () => QuickIOPath.Combine("", null); Action actionNoParameter = () => QuickIOPath.Combine(); actionNullParameter.ShouldThrow <ArgumentNullException>(); actionNoParameter.ShouldThrow <ArgumentNullException>(); }
/// <summary> /// Determined all sub system entries of a directory /// </summary> /// <param name="uncDirectoryPath">Path of the directory</param> /// <param name="pattern">Search pattern. Uses Win32 native filtering.</param> /// <param name="searchOption"><see cref="SearchOption"/></param> /// <param name="enumerateOptions">The enumeration options for exception handling</param> /// <returns>Collection of <see cref="QuickIODirectoryInfo"/></returns> /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception> internal static IEnumerable <QuickIOFileSystemEntry> EnumerateFileSystemEntries(String uncDirectoryPath, String pattern = QuickIOPatterns.PathMatchAll, SearchOption searchOption = SearchOption.TopDirectoryOnly, QuickIOEnumerateOptions enumerateOptions = QuickIOEnumerateOptions.None) { Contract.Requires(!String.IsNullOrWhiteSpace(uncDirectoryPath)); Contract.Ensures(Contract.Result <IEnumerable <QuickIOFileSystemEntry> >() != null); return (from pair in EnumerateWin32FileSystemEntries(uncDirectoryPath, pattern, searchOption, enumerateOptions) let parentDirectory = pair.Item1 let win32Entry = pair.Item2 let fullpath = QuickIOPath.Combine(parentDirectory, win32Entry.Name) select new QuickIOFileSystemEntry(fullpath, win32Entry.FileSystemEntryType, win32Entry.Attributes, win32Entry.Bytes)); }
//[InlineData( "_TestFolders/ExistingFolder", "*.txt", 1 )] //[InlineData( "_TestFolders/ExistingFolder", "*.TXT", 1 )] public void QuickIODirectoryInfo_EnumerateFilesCount(bool isRelative, string path, string pattern, int expected) { if (isRelative) { path = QuickIOPath.Combine(CurrentPath(), path); } QuickIODirectoryInfo directoryInfo = new QuickIODirectoryInfo(path); IEnumerable <QuickIOFileInfo> result = (pattern == null ? directoryInfo.EnumerateFiles() : directoryInfo.EnumerateFiles(pattern)); List <QuickIOFileInfo> list = result.ToList(); list.Count.Should().Be(expected); }
/// <summary> /// Determines the statistics of the given directory. This includes the number of files, folders and the total size in bytes. /// </summary> /// <param name="uncDirectoryPath">Path to the directory to generate the statistics.</param> /// <returns>Provides the statistics of the directory</returns> /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception> public static QuickIOFolderStatisticResult EnumerateDirectoryStatistics(String uncDirectoryPath) { Contract.Requires(!String.IsNullOrWhiteSpace(uncDirectoryPath)); Contract.Ensures(Contract.Result <QuickIOFolderStatisticResult>() != null); UInt64 fileCount = 0; UInt64 folderCount = 0; UInt64 totalSize = 0; Stack <string> directoryPathStack = new Stack <string>(); directoryPathStack.Push(uncDirectoryPath); while (directoryPathStack.Count > 0) { folderCount++; string currentDirectory = directoryPathStack.Pop(); foreach (Win32FileSystemEntry systemEntry in new Win32FileHandleCollection(QuickIOPath.Combine(currentDirectory, QuickIOPatterns.PathMatchAll))) { // Create hit for current search result var resultPath = QuickIOPath.Combine(currentDirectory, systemEntry.Name); // if it's a file, add to the collection if (systemEntry.IsFile) { fileCount++; totalSize += systemEntry.Bytes; } else { directoryPathStack.Push(resultPath); } } } // Return result; return(new QuickIOFolderStatisticResult(folderCount, fileCount, totalSize)); }
public void QuickIOPathInfoFile() { string parent = QuickIOPath.Combine(CurrentPath(), "_TestFiles"); string fullpath = QuickIOPath.Combine(CurrentPath(), "_TestFiles", "ExistingTestFile.txt"); string fullPathUnc = QuickIOPath.ToPathUnc(fullpath); string root = QuickIOPath.GetPathRoot(fullpath); QuickIOPathInfo pi = new QuickIOPathInfo(fullpath); pi.Should().NotBe(null); pi.Name.Should().Be("ExistingTestFile.txt"); pi.FullName.Should().Be(fullpath); pi.FullNameUnc.Should().Be(fullPathUnc); pi.Parent.Should().Be(parent); pi.Root.Should().Be(root); pi.IsRoot.Should().Be(false); pi.FindData.Should().NotBe(null); InternalHelpers.ContainsFileAttribute(pi.Attributes, FileAttributes.Directory).Should().Be(false); pi.Exists.Should().Be(true); pi.SystemEntryType.Should().Be(QuickIOFileSystemEntryType.File); }
/// <summary> /// Determined all files of a directory /// </summary> /// <param name="uncDirectoryPath">Path of the directory</param> /// <param name="pattern">Search pattern. Uses Win32 native filtering.</param> /// <param name="searchOption"><see cref="SearchOption"/></param> /// <param name="enumerateOptions">The enumeration options for exception handling</param> /// <returns>Collection of files</returns> /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception> internal static IEnumerable <QuickIOFileInfo> EnumerateFiles(String uncDirectoryPath, String pattern = QuickIOPatterns.PathMatchAll, SearchOption searchOption = SearchOption.TopDirectoryOnly, QuickIOEnumerateOptions enumerateOptions = QuickIOEnumerateOptions.None) { Contract.Requires(!String.IsNullOrWhiteSpace(uncDirectoryPath)); Contract.Ensures(Contract.Result <IEnumerable <QuickIOFileInfo> >() != null); foreach (Win32FileSystemEntry systemEntry in new Win32FileHandleCollection(QuickIOPath.Combine(uncDirectoryPath, pattern))) { // Create hit for current search result var resultPath = QuickIOPath.Combine(uncDirectoryPath, systemEntry.Name); // Check for Directory if (systemEntry.IsFile) { yield return(new QuickIOFileInfo(resultPath, systemEntry.FindData)); } else if (/* it's already a directory here */ searchOption == SearchOption.AllDirectories) { foreach (var match in EnumerateFiles(resultPath, pattern, searchOption, enumerateOptions)) { yield return(match); } } } }