Beispiel #1
0
    /// <summary>
    /// Will delete a readonly folder and all sub-folders and files contained therein.
    /// </summary>
    /// <param name="Path">The path.</param>
    static void ReadOnlyFolderDelete(string Path)
    {
        DirectoryInfo         di      = new DirectoryInfo(Path);
        Stack <DirectoryInfo> folders = new Stack <DirectoryInfo>();
        DirectoryInfo         folder;

        // Add to the stack.
        folders.Push(di);
        AccessManager.RemoveFolderKey(di.FullName);
        while (folders.Count > 0)
        {
            // Get the folder and set all attributes to normal.
            folder            = folders.Pop();
            folder.Attributes = FileAttributes.Normal;

            // Add to the stack.
            foreach (DirectoryInfo dir in folder.GetDirectories())
            {
                folders.Push(dir);
                AccessManager.RemoveFolderKey(dir.FullName);
            }

            // Set and delete all of the files.
            foreach (FileInfo fi in folder.GetFiles())
            {
                fi.Attributes = FileAttributes.Normal;
                fi.Delete();
            }
        }

        // Delete the folder and all sub-folders.
        di.Delete(true);
    }