Directoy.Delete(路径, true) -> IOException:目录不为空 -> Win7上不同的所有者
卸载时,我在尝试删除目录时收到 IOException。 '目录不为空'。我尝试了下面列出的不同方法,但没有任何效果。 留下的文件(并且无法删除)具有不同的所有者。 可以删除的文件的所有者为“SYSTEM”。引发异常的文件的所有者为“Administrators(PC_Name\Administrators)” “系统”文件已由 installshield 安装程序 (MSI) 写入,而其他文件由我的应用程序写入,由 installshield 启动并提升为管理员...
我如何强制删除此文件夹/文件?
//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);
}
编辑:抱歉,忘记了这一点:
在出现问题的文件夹上,在安装时,我给予“用户”帐户完全控制权:
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两个常见选项可能会导致此问题:
1) 您的应用程序以非管理员用户身份运行。
2) 您的应用程序仍然以某种方式对其创建的那些文件持有锁定。
第一个选项不太可能,因为文件是由它创建的,所以让我们集中讨论第二个选项。
为了轻松识别此问题,在应用程序运行时(如果进程很快,您可以使用调试器暂停它),请尝试手动删除这些文件。如果失败,则意味着应用程序仍然有锁。
如果是这种情况,请确保关闭所有文件流并正确处理它们。
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.