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

Delete Undeletable Folders under Vista or Windows 7

By , 28 Jun 2011
 
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
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.

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membervicmem27-Jan-13 4:38 
QuestionUnexpected problem and a fixmemberAlexms7-Jul-11 6:02 
AnswerRe: Unexpected problem and a fixmemberPascal Ganaye15-Sep-11 2:22 
QuestionCan't this be done using Windows Explorer?memberGeorge Swan29-Jun-11 12:18 
AnswerRe: Can't this be done using Windows Explorer? [modified]memberPascal Ganaye29-Jun-11 12:42 
QuestionMy vote of 5memberFilip D'haene28-Jun-11 9:56 
GeneralMy vote of 5memberPascal Ganaye28-Jun-11 8:20 
GeneralRe: My vote of 5mentorDaveAuld28-Jun-11 8:43 
GeneralRe: My vote of 5memberPascal Ganaye28-Jun-11 11:24 
GeneralRe: My vote of 5memberRavi Bhavnani28-Jun-11 9:38 

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

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