Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#
Article

Delete Undeletable Folders under Vista or Windows 7

Rate me:
Please Sign up or sign in to vote.
4.73/5 (18 votes)
28 Jun 2011CPOL1 min read 36.1K   2.1K   34   10
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.

C#
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.

C#
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.

C#
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.

XML
<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.

C#
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)


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

 
GeneralMy vote of 5 Pin
vicmem27-Jan-13 4:38
vicmem27-Jan-13 4:38 
QuestionUnexpected problem and a fix Pin
Alexms7-Jul-11 6:02
Alexms7-Jul-11 6:02 
AnswerRe: Unexpected problem and a fix Pin
Pascal Ganaye15-Sep-11 2:22
Pascal Ganaye15-Sep-11 2:22 
QuestionCan't this be done using Windows Explorer? Pin
George Swan29-Jun-11 12:18
mveGeorge Swan29-Jun-11 12:18 
AnswerRe: Can't this be done using Windows Explorer? [modified] Pin
Pascal Ganaye29-Jun-11 12:42
Pascal Ganaye29-Jun-11 12:42 
QuestionMy vote of 5 Pin
Filip D'haene28-Jun-11 9:56
Filip D'haene28-Jun-11 9:56 
GeneralMy vote of 5 Pin
Pascal Ganaye28-Jun-11 8:20
Pascal Ganaye28-Jun-11 8:20 
GeneralRe: My vote of 5 Pin
DaveAuld28-Jun-11 8:43
professionalDaveAuld28-Jun-11 8:43 
GeneralRe: My vote of 5 Pin
Pascal Ganaye28-Jun-11 11:24
Pascal Ganaye28-Jun-11 11:24 
GeneralRe: My vote of 5 Pin
Ravi Bhavnani28-Jun-11 9:38
professionalRavi Bhavnani28-Jun-11 9:38 

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.