Click here to Skip to main content
15,885,278 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Access
Tip/Trick

Deleting protected folders in Windows 7

Rate me:
Please Sign up or sign in to vote.
4.82/5 (13 votes)
25 Mar 2011CPOL 31.5K   16   10
Deleting protected folders in Windows 7
I had trouble lately trying to get rid of a lot of folders from a previous windows installation.
I had
d:\temp\windows
d:\temp\program files
...
This had to do with ownership and access rights.
Running this program did the trick.
Perhaps someone will want to push it a bit further and make a real utility of it.

USE AT YOUR OWN RISKS, THIS PROGRAM IS LETHAL FOR YOUR HARD DISKS

C#
class Program
{
    static void Main(string[] args)
    {
        var path = args[0];
        DeleteFolder(new System.IO.DirectoryInfo(path));
    }

    private static void DeleteFolder(DirectoryInfo directoryInfo)
    {
        GrantAccess(directoryInfo.FullName);
        try
        {
            foreach (DirectoryInfo d in directoryInfo.GetDirectories())
            {
                DeleteFolder(d);
            }
        }
        catch { }

        try
        {
            foreach (FileInfo f in directoryInfo.GetFiles())
            {
                GrantAccess(f.FullName);
                try
                {
                    f.Delete();
                    Console.WriteLine("Deleted File {0}", f.FullName);
                }
                catch { }
            }
        }
        catch { }

        try
        {
            directoryInfo.Delete(true);
            Console.WriteLine("Deleted Folder {0}", directoryInfo.FullName);
        }
        catch { }

    }

    private static void GrantAccess(string filepath)
    {

        var fs = File.GetAccessControl(filepath);
        var sid = fs.GetOwner(typeof(SecurityIdentifier));
        var ntAccount = new NTAccount(Environment.UserDomainName, Environment.UserName);
        try
        {
            var currentRules = fs.GetAccessRules(true, false,typeof(NTAccount));
            foreach (var r in currentRules.OfType<FileSystemAccessRule>())
            {
                Console.WriteLine(r.AccessControlType + " " + r.FileSystemRights);
            }
            var newRule = new FileSystemAccessRule(
                ntAccount, FileSystemRights.FullControl,
                AccessControlType.Allow);
            fs.AddAccessRule(newRule);
            File.SetAccessControl(filepath, fs);
        }
        catch { }
        finally { fs=null; sid=null; ntAccount=null;} 
    }
}


-----------------------
I would recommend to add the finally{fs=null; sid=null; ntAccount=null;} to the function in order to ensure that the objects obtained such powerful control over file system will be disposed/finalized as soon as possible in any scenario. (added by Alex Bell)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions

 
GeneralRe: No I am not running it, I am trying to compile it (VS 2010) Pin
FDW6-Dec-11 6:39
FDW6-Dec-11 6:39 
GeneralNo I am not running it, I am trying to compile it (VS 2010) Pin
FDW6-Dec-11 6:39
FDW6-Dec-11 6:39 
GeneralI like it, but, why do I get "Error 3 'File' does not contai... Pin
FDW6-Dec-11 3:07
FDW6-Dec-11 3:07 
GeneralRe: I am not sure, I'd say perhaps, you're not running the progr... Pin
Pascal Ganaye6-Dec-11 5:28
Pascal Ganaye6-Dec-11 5:28 
GeneralHere is a link to an example that implements a "RemoveReadon... Pin
Keith.Badeau8-May-11 1:25
Keith.Badeau8-May-11 1:25 
GeneralReason for my vote of 5 Hate Vista "security" therefre love ... Pin
Gunnar Sigfusson30-Mar-11 13:54
Gunnar Sigfusson30-Mar-11 13:54 
GeneralSetting local variables to null "to ensure that [they] will ... Pin
Richard Deeming29-Mar-11 7:29
mveRichard Deeming29-Mar-11 7:29 
GeneralReason for my vote of 5 5/5 Pin
Abdul Quader Mamun28-Mar-11 16:26
Abdul Quader Mamun28-Mar-11 16:26 
GeneralGood work. Pin
Abdul Quader Mamun28-Mar-11 16:26
Abdul Quader Mamun28-Mar-11 16:26 
GeneralReason for my vote of 5 Interesting solution, though associa... Pin
DrABELL24-Mar-11 5:36
DrABELL24-Mar-11 5:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.