/// <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);
            }
        }
Example #2
0
        /// <summary>
        /// Deletes all files in the given directory.
        /// <para>If recursive flag is `true` all subdirectories and files will be removed.</para>
        /// If recursive flag is `false` the method will fail if the target is not empty.
        /// </summary>
        /// <param name="directoryInfo">Info of directory to clear</param>
        /// <param name="recursive">If <paramref name="recursive"/> is true then all subfolders are also deleted.</param>
        /// <remarks>Function loads every file and attribute. Alls read-only flags will be removed before removing.</remarks>
        public static void DeleteDirectory(DirectoryDetail directoryInfo, bool recursive = false)
        {
            // Contents
            if (recursive)
            {
                // search all contents
                var subFiles = FindPaths(directoryInfo.FullNameUnc, pathFormatReturn: UncOrRegular.UNC);
                foreach (var item in subFiles)
                {
                    DeleteFileUnc(item);
                }

                var subDirs = EnumerateDirectories(directoryInfo.PathInfo);
                foreach (var subDir in subDirs)
                {
                    DeleteDirectory(subDir, true);
                }
            }

            // Remove specified
            var removed    = Win32SafeNativeMethods.RemoveDirectory(directoryInfo.FullNameUnc);
            var win32Error = Marshal.GetLastWin32Error();

            if (!removed)
            {
                NativeExceptionMapping(directoryInfo.FullName, win32Error);
            }
        }