コード例 #1
0
        public void TryGetShareUncRootPath(string test, string rootExpected, bool parseResultExpected)
        {
            string root;

            QuickIOPath.TryGetShareUncRootPath(test, out root).Should().Be(parseResultExpected);
            root.Should().Be(rootExpected);
        }
コード例 #2
0
        /// <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);
            }
        }
コード例 #3
0
        public void GetRandomFileName()
        {
            string test = QuickIOPath.GetRandomFileName();

            test.Should().NotBeNullOrEmpty();
            test.Should().NotBeNullOrWhiteSpace();
        }
コード例 #4
0
        /// <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));
        }
コード例 #5
0
        /// <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);
                    }
                }
            }
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        public void Combine_ArgumentNullException()
        {
            Action actionNullParameter = () => QuickIOPath.Combine("", null);
            Action actionNoParameter   = () => QuickIOPath.Combine();

            actionNullParameter.ShouldThrow <ArgumentNullException>();
            actionNoParameter.ShouldThrow <ArgumentNullException>();
        }
コード例 #8
0
 public void IsRootLocalRegular_TrueTestsLoop(string test, bool expected)
 {
     // True
     foreach (var c in test)
     {
         QuickIOPath.IsRootLocalRegular(c + @":\").Should().Be(expected);
     }
 }
コード例 #9
0
        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);
        }
コード例 #10
0
        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");
            }
        }
コード例 #11
0
 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));
     }
 }
コード例 #12
0
        /// <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));
        }
コード例 #13
0
        /// <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());
                        }
                    }
                }
            }
        }
コード例 #14
0
        //[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);
        }
コード例 #15
0
        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);
            }
            }
        }
コード例 #16
0
        /// <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));
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        /// <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);
                    }
                }
            }
        }
コード例 #19
0
 public void IsShareRegular(string test, bool expected)
 {
     QuickIOPath.IsShareRegular(test).Should().Be(expected);
 }
コード例 #20
0
 public void Clean(string p1, string expected)
 {
     QuickIOPath.Clean(p1).Should().Be(expected);
 }
コード例 #21
0
 public void IsRootLocalRegular_FalseTests(string test, bool expected)
 {
     QuickIOPath.IsRootLocalRegular(test).Should().Be(expected);
 }
コード例 #22
0
 public void IsRootShareUnc(string test, bool expected)
 {
     QuickIOPath.IsRootShareUnc(test).Should().Be(expected);
 }
コード例 #23
0
 public void IsValidFolderName()
 {
     QuickIOPath.IsValidFolderName(new string( 'a', QuickIOPath.MaxFolderNameLength + 1 )).Should().Be(false);
 }
コード例 #24
0
 public void IsValidFolderChar(char test, bool expected)
 {
     QuickIOPath.IsValidFolderChar(test).Should().Be(expected);
 }
コード例 #25
0
 public void IsValidDriveLetter(char test, bool expected)
 {
     QuickIOPath.IsValidDriveLetter(test).Should().Be(expected);
 }
コード例 #26
0
 public void GetPathRoot(string test, string expected)
 {
     QuickIOPath.GetPathRoot(test).Should().Be(expected);
 }
コード例 #27
0
 public void IsRelative(string test, bool expected)
 {
     QuickIOPath.IsRelative(test).Should().Be(expected, because: test);
 }
コード例 #28
0
 public void IsValidShareName(string test, bool expected)
 {
     QuickIOPath.IsValidShareName(test).Should().Be(expected);
 }
コード例 #29
0
 public void ToPathUnc(string test, string expected)
 {
     QuickIOPath.ToPathUnc(test).Should().Be(expected);
 }
コード例 #30
0
 public void IsLocalUnc(string test, bool expected)
 {
     QuickIOPath.IsLocalUnc(test).Should().Be(expected);
 }