Click here to Skip to main content
Licence CPOL
First Posted 28 Jun 2011
Views 6,809
Downloads 915
Bookmarked 27 times

Delete Undeletable Folders under Vista or Windows 7

By | 28 Jun 2011 | Article
A small utility that will help you get rid of the hard to remove protected or system folders
VistaDeleteFolder.png

Introduction

If you're like me, you've been confronted by a recalcitrant folder that doesn't want to go to the bin.

The idea behind this article is to write a little program that will delete this folder for us. How hard can it be?

Background

This article is a follow up of the original Tip and Trick: Deleting protected folders in Windows 7.

Using the application

  1. Run the executable: VistaDeleteFolder.exe
  2. Carefully select the folder you want to delete
  3. Press the start button

Under the Hood

Hey, this is CodeProject. You want to see some source, don't you?

The main idea behind the program is to fix the access rights for every file and folder.

private static void FixAccess(FileSystemSecurity sec)
{
    string currentUser = WindowsIdentity.GetCurrent().Name;
    sec.AddAccessRule(new FileSystemAccessRule
	(currentUser, FileSystemRights.FullControl, AccessControlType.Allow));
}

I also clear all the existing rules in the hope that it will avoid conflicts.

foreach (FileSystemAccessRule fsar in sec.GetAccessRules
	(true, true, typeof(System.Security.Principal.NTAccount)).OfType<filesystemaccessrule />().ToArray())
{
    sec.RemoveAccessRuleAll(fsar);
}

Unfortunately, most of this was failing at the beginning and I could not find out why. I thought the solution was to fix the ownership of the file.

private static void FixOwner(FileSystemSecurity sec)
{
    var sid = sec.GetOwner(typeof(SecurityIdentifier));
    string currentUser = WindowsIdentity.GetCurrent().Name;
    var ntAccount = new NTAccount(currentUser);
    sec.SetOwner(ntAccount);
}

Unfortunately, that was not enough. The application needs to be run in administrative mode. This can be enforced by changing the executable manifest file.

<asmv1:assembly manifestVersion="1.0" ...>
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
  <security>
  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">


  <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

  </requestedPrivileges>
  </security>
  </trustInfo>
...  
</asmv1:assembly>

The sad thing is at that point it was still not working. I was hitting an exception, the change of owner was refused for some unexplained reason. It continued Googling for the exception and stumbled upon http://processprivileges.codeplex.com/. Using their library is easy. I added this line.

using (new ProcessPrivileges.PrivilegeEnabler
	(Process.GetCurrentProcess(), Privilege.TakeOwnership))
{
    <original code here>
}

I did not study much how this library does it, but it did the trick. The recalcitrant folder is gone.

History

  • 28 June 2011: Improved and released as an article
  • 22 March 2011: First release as a tip and trick

License

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

About the Author

Pascal Ganaye

Web Developer

United Kingdom United Kingdom

Member

I am a French programmer.
These days I spend most of my time within the .NET framework.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionUnexpected problem and a fix PinmemberAlexms6:02 7 Jul '11  
AnswerRe: Unexpected problem and a fix PinmemberPascal Ganaye2:22 15 Sep '11  
QuestionCan't this be done using Windows Explorer? PinmemberGeorge Swan12:18 29 Jun '11  
AnswerRe: Can't this be done using Windows Explorer? [modified] PinmemberPascal Ganaye12:42 29 Jun '11  
QuestionMy vote of 5 PinmemberFilip D'haene9:56 28 Jun '11  
GeneralMy vote of 5 PinmemberPascal Ganaye8:20 28 Jun '11  
GeneralRe: My vote of 5 PinmentorDaveAuld8:43 28 Jun '11  
GeneralRe: My vote of 5 PinmemberPascal Ganaye11:24 28 Jun '11  
GeneralRe: My vote of 5 PinmemberRavi Bhavnani9:38 28 Jun '11  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 28 Jun 2011
Article Copyright 2011 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid