3

On uninstalling, I get a IOException when trying to delete a directory. 'The directory is not empty'. I tried different methods, listed below, but nothing works. The files that are left behind (and cannot be deleted) have a different owner. Files that can be deleted have owner 'SYSTEM'. Files that throw an exception have owner 'Administrators(PC_Name\Administrators)' 'System' files have been written by the installshield installer (MSI), while the other files are written by my app, launched by installshield and elevated to administrator ...

How can I force a delete of this folder / files?

    //http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true
    public static bool DeleteDirectory(string target_dir)
    {
        bool result = false;

        string[] files = Directory.GetFiles(target_dir);
        string[] dirs = Directory.GetDirectories(target_dir);

        foreach (string file in files)
        {
            File.SetAttributes(file, FileAttributes.Normal);
            File.Delete(file);
        }

        foreach (string dir in dirs)
        {
            DeleteDirectory(dir);
        }

        Directory.Delete(target_dir, false);

        return result;
    }

    //http://stackoverflow.com/questions/611921/how-do-i-delete-a-directory-with-read-only-files-in-c
    private static void DeleteFileSystemInfo(FileSystemInfo fsi) 
    { 
        fsi.Attributes = FileAttributes.Normal; 
        var di = fsi as DirectoryInfo; 
        if (di != null) 
        { 
            foreach (var dirInfo in di.GetFileSystemInfos())             
                DeleteFileSystemInfo(dirInfo); } 

        fsi.Delete(); 
    }

    //http://stackoverflow.com/questions/611921/how-do-i-delete-a-directory-with-read-only-files-in-c
    public static void ForceDeleteDirectory(string path)
    {
        DirectoryInfo root;
        Stack<DirectoryInfo> fols;
        DirectoryInfo fol;
        fols = new Stack<DirectoryInfo>();
        root = new DirectoryInfo(path);
        fols.Push(root);
        while (fols.Count > 0)
        {
            fol = fols.Pop();
            fol.Attributes = fol.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden);
            foreach (DirectoryInfo d in fol.GetDirectories())
            {
                fols.Push(d);
            }
            foreach (FileInfo f in fol.GetFiles())
            {
                f.Attributes = f.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden);
                f.Delete();
            }
        }
        root.Delete(true);
    }

EDIT: Sorry, forgot this :

On the folder that is giving problems, on installation, I'm giving the 'Users' account full control :

        System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(
            System.Security.Principal.WellKnownSidType.BuiltinUsersSid, null);
        System.Security.Principal.NTAccount acct = sid.Translate(typeof(System.Security.Principal.NTAccount))
            as System.Security.Principal.NTAccount;
        string usr = acct.ToString();

        DirectoryInfo info = new DirectoryInfo(dir);
        DirectorySecurity ds = info.GetAccessControl();

        ds.AddAccessRule(new FileSystemAccessRule(usr, FileSystemRights.FullControl, AccessControlType.Allow));
        ds.AddAccessRule(new FileSystemAccessRule(usr, FileSystemRights.FullControl,
            InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly,
            AccessControlType.Allow));
        info.SetAccessControl(ds);
tshepang
  • 12,111
  • 21
  • 91
  • 136
Run CMD
  • 2,937
  • 3
  • 36
  • 61
  • Are you doing something weird with the permissions when you install? Normally SYSTEM has rights to delete files, but apparently that's turned off for your install directory. – Joe White Nov 15 '11 at 12:52
  • Nothing special as far as I know ..? Everything that can be deleted is installed by installshield. The files that can't be deleted are those that I've written myself during installation..... Even if I right click the file and choose 'Security', both SYSTEM as ADMINISTRATORS have 'Full Control' ?? – Run CMD Nov 15 '11 at 13:03
  • Hi Joe, sorry forgot to mention I'm giving special permissions on the folder on installation (see question) – Run CMD Nov 15 '11 at 14:06
  • any final solution with full source code sample working about it ? – Kiquenet Jul 18 '13 at 12:00

1 Answers1

1

There are two common options that can cause this problem:
1) Your application running as a non-Administrator user.
2) Your application still somehow holds a lock on those files created by it.

The first option is not probable, because the files were created by it, so let us center on the second one. To easily identify this problem, while application running(in case process is fast, you can pause it using debugger), try to delete those files manually. If you fail, it means the application still has a lock.

If that's the case, make sure you close all file streams and dispose of them correctly.

Svarog
  • 2,188
  • 15
  • 21
  • While uninstalling and error message is shown, I can delete the files manually without a problem. I also have written a simple test program to delete the folder with Directory.Delete and that works too without a single problem ... – Run CMD Nov 15 '11 at 14:12