Click here to Skip to main content
Click here to Skip to main content

Deleting protected folders in Windows 7

, 25 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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
 
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)

Share

About the Author

Pascal Ganaye
Software Developer (Senior)
United Kingdom United Kingdom
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) PinmemberFDW6-Dec-11 7:39 
GeneralNo I am not running it, I am trying to compile it (VS 2010) PinmemberFDW6-Dec-11 7:39 
GeneralI like it, but, why do I get "Error 3 'File' does not contai... PinmemberFDW6-Dec-11 4:07 
GeneralRe: I am not sure, I'd say perhaps, you're not running the progr... PinmemberPascal Ganaye6-Dec-11 6:28 
GeneralHere is a link to an example that implements a "RemoveReadon... PinmemberKeith.Badeau8-May-11 2:25 
GeneralReason for my vote of 5 Hate Vista "security" therefre love ... PinmemberGunnar Sigfusson30-Mar-11 14:54 
GeneralSetting local variables to null "to ensure that [they] will ... PinmemberRichard Deeming29-Mar-11 8:29 
GeneralReason for my vote of 5 5/5 PinmemberAbdul Quader Mamun28-Mar-11 17:26 
GeneralGood work. PinmemberAbdul Quader Mamun28-Mar-11 17:26 
GeneralReason for my vote of 5 Interesting solution, though associa... PinmemberDrABELL24-Mar-11 6:36 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 25 Mar 2011
Article Copyright 2011 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid