Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Delete Undeletable Folders under Vista or Windows 7

4.73/5 (18 votes)
28 Jun 2011CPOL1 min read 36.8K   2.1K  
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)