A Simple "Take Ownership" Windows Form Utility

From time to time we all run across folders and files that we cannot move or delete because their security descriptors were set from a previous installation (or any of a dozen other reasons). This .NET Windows Form utility uses the FileSecurity, IdentityReference and NTAccount classes. It will iterate over all the current access rules on the folder, displaying each in a label, and then it will add a new rule for the current Windows User granting "Full Control".

This will allow you to delete any of the annoying files or folders that you cannot normally access. I also added a button that will go to your VisualStudio\10\ folder in your AppData folder, grant full control, and then automatically delete all the .tbd files there. These .tbd files can cause Visual Studio to take a very long time to load if the Toolbox is visible. You may notice this issue particularly if you have third party controls installed such as the Telerik suite. Manual deletion of these files via a batch file does not work.



The code is extremely simple:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;

namespace OwnFiles
{
    public partial class Form1 : Form
    {
         public Form1()
        {
             InitializeComponent();
        }

         private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();
             if (dr == DialogResult.OK)
            {
                string filePath = folderBrowserDialog1.SelectedPath;
                 GrantFullControl(filePath);

            }
        }

         private void GrantFullControl(string filepath)
        {
            FileSecurity  fs = File.GetAccessControl(filepath);
            IdentityReference 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>())
                {
                    label1.Text =r.AccessControlType + ": " + r.FileSystemRights.ToString();
                }
                var newRule = new FileSystemAccessRule(ntAccount, FileSystemRights.FullControl, AccessControlType.Allow);
                fs.AddAccessRule(newRule);
                File.SetAccessControl(filepath, fs);
            }
            catch (Exception ex)
            {
                label1.Text = ex.Message;
             }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string path = @"C:\Users\" + Environment.UserName +@"\AppData\Local\Microsoft\VisualStudio\10.0\";

            GrantFullControl(path);
            string[] txtList = Directory.GetFiles(path, "*.tbd");

            foreach (string s in txtList)
            {
                string target = Path.Combine(path, s);
                File.Delete(target);
            }
        }
    }
}

Once you've built the downloadable Visual Studio 2010 solution in Release mode, you can create a start menu or desktop shortcut to the executable and use it whenever you need it.

By Peter Bromberg   Popularity  (2240 Views)