Reputation: 3035
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);
Upvotes: 3
Views: 3574
Reputation: 2208
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.
Upvotes: 1