public void TryGetShareUncRootPath(string test, string rootExpected, bool parseResultExpected) { string root; QuickIOPath.TryGetShareUncRootPath(test, out root).Should().Be(parseResultExpected); root.Should().Be(rootExpected); }
/// <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); } }
public void GetRandomFileName() { string test = QuickIOPath.GetRandomFileName(); test.Should().NotBeNullOrEmpty(); test.Should().NotBeNullOrWhiteSpace(); }
/// <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>(); }
public void IsRootLocalRegular_TrueTestsLoop(string test, bool expected) { // True foreach (var c in test) { QuickIOPath.IsRootLocalRegular(c + @":\").Should().Be(expected); } }
public void TryGetServerAndShareNameFromLocation(string test, string serverNameExpected, string shareNameExpected, bool parseResultExpected) { string serverName, shareName; QuickIOPath.TryParseShare(test, QuickIOPathType.Regular, out serverName, out shareName).Should().Be(parseResultExpected); serverName.Should().Be(serverNameExpected); shareName.Should().Be(shareNameExpected); }
public void GetDirectoryName(string test, string expected, bool differsFromSystemIO) { string result = QuickIOPath.GetDirectoryName(test); result.Should().Be(expected); if (!differsFromSystemIO) { result.Should().Be(System.IO.Path.GetDirectoryName(test), "Syste.IO differs"); } }
public void GetFullPath() { string[] pathList = new string[] { @"_TestFiles\TextFile.txt", @"C:\temp" }; foreach (var entry in pathList) { QuickIOPath.GetFullPath(entry).Should().Be(System.IO.Path.GetFullPath(entry)); } }
/// <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)); }
/// <summary> /// Creates a new directory. If <paramref name="recursive"/> is false, the parent directory must exists. /// </summary> /// <param name="uncDirectoryPath">Directory path</param> /// <param name="recursive">If <paramref name="recursive"/> is false, the parent directory must exist.</param> /// <exception cref="PathAlreadyExistsException">The specified path already exists.</exception> /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception> public static void CreateDirectory(string uncDirectoryPath, bool recursive = false) { Contract.Requires(!String.IsNullOrEmpty(uncDirectoryPath)); // cancel if path exists if (Exists(uncDirectoryPath)) { return; } // cancel if requested path is root if (QuickIOPath.IsRoot(uncDirectoryPath)) { throw new InvalidOperationException("A root directory cannot be created."); } // create parent directory if accepted if (recursive) { string parent = QuickIOPath.GetDirectoryName(uncDirectoryPath); if (parent == null) { throw new InvalidOperationException("Parent directory does not exists and cannot be created."); } Stack <string> stack = new Stack <string>(); stack.Push(parent); while (stack.Count > 0) { string currentDirectory = stack.Pop(); if (QuickIOPath.IsRoot(currentDirectory)) { if (!QuickIOPath.Exists(currentDirectory)) { throw new InvalidOperationException("A root directory cannot be created."); } } else { // no root path here if (!Win32SafeNativeMethods.CreateDirectory(currentDirectory, IntPtr.Zero)) { Win32ErrorCodes.NativeExceptionMapping(currentDirectory, Marshal.GetLastWin32Error()); } } } } }
//[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); }
public static Win32FindData GetFindDataFromPath(string fullpath, QuickIOFileSystemEntryType?estimatedFileSystemEntryType = null) { Contract.Requires(fullpath != null); //Changed to allow paths which do not exist: //Contract.Ensures( Contract.Result<Win32FindData>() != null ); if (!QuickIOPath.Exists(fullpath)) { return(null); } Win32FindData win32FindData = SafeGetFindDataFromPath(fullpath); if (win32FindData == null) { //Changed to allow paths which do not exist: //throw new PathNotFoundException( fullpath ); return(null); } // Check for correct type switch (estimatedFileSystemEntryType) { case QuickIOFileSystemEntryType.Directory: { // Check for directory flag if (InternalHelpers.ContainsFileAttribute(win32FindData.dwFileAttributes, FileAttributes.Directory)) { return(win32FindData); } throw new UnmatchedFileSystemEntryTypeException(QuickIOFileSystemEntryType.Directory, QuickIOFileSystemEntryType.File, fullpath); } case QuickIOFileSystemEntryType.File: { // Check for directory flag if (!InternalHelpers.ContainsFileAttribute(win32FindData.dwFileAttributes, FileAttributes.Directory)) { return(win32FindData); } throw new UnmatchedFileSystemEntryTypeException(QuickIOFileSystemEntryType.File, QuickIOFileSystemEntryType.Directory, fullpath); } case null: default: { return(win32FindData); } } }
/// <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); } } } }
public void IsShareRegular(string test, bool expected) { QuickIOPath.IsShareRegular(test).Should().Be(expected); }
public void Clean(string p1, string expected) { QuickIOPath.Clean(p1).Should().Be(expected); }
public void IsRootLocalRegular_FalseTests(string test, bool expected) { QuickIOPath.IsRootLocalRegular(test).Should().Be(expected); }
public void IsRootShareUnc(string test, bool expected) { QuickIOPath.IsRootShareUnc(test).Should().Be(expected); }
public void IsValidFolderName() { QuickIOPath.IsValidFolderName(new string( 'a', QuickIOPath.MaxFolderNameLength + 1 )).Should().Be(false); }
public void IsValidFolderChar(char test, bool expected) { QuickIOPath.IsValidFolderChar(test).Should().Be(expected); }
public void IsValidDriveLetter(char test, bool expected) { QuickIOPath.IsValidDriveLetter(test).Should().Be(expected); }
public void GetPathRoot(string test, string expected) { QuickIOPath.GetPathRoot(test).Should().Be(expected); }
public void IsRelative(string test, bool expected) { QuickIOPath.IsRelative(test).Should().Be(expected, because: test); }
public void IsValidShareName(string test, bool expected) { QuickIOPath.IsValidShareName(test).Should().Be(expected); }
public void ToPathUnc(string test, string expected) { QuickIOPath.ToPathUnc(test).Should().Be(expected); }
public void IsLocalUnc(string test, bool expected) { QuickIOPath.IsLocalUnc(test).Should().Be(expected); }