Click here to Skip to main content
12,248,681 members (44,391 online)
Click here to Skip to main content
Add your own
alternative version

Stats

18K views
16 bookmarked
Posted

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)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

You may also be interested in...

Comments and Discussions

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

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