在 C# 中,如何检查 File.Delete() 是否会在不尝试的情况下成功?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1444153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check whether File.Delete() will succeed without trying it, in C#?
提问by Dylan Beattie
In C#, System.IO.File.Delete(filePath) will either delete the specified file, or raise an exception. If the current user doesn't have permission to delete the file, it'll raise an UnauthorizedAccessException.
在 C# 中, System.IO.File.Delete(filePath) 要么删除指定的文件,要么引发异常。如果当前用户没有删除文件的权限,则会引发 UnauthorizedAccessException。
Is there some way that I can tell ahead of time whether the delete is likely to throw an UnauthorizedAccessException or not (i.e. query the ACL to see whether the current thread's identity has permission to delete the specified file?)
有什么方法可以提前告诉我删除是否可能抛出 UnauthorizedAccessException (即查询 ACL 以查看当前线程的身份是否有权删除指定的文件?)
I'm basically looking to do:
我基本上想做:
if (FileIsDeletableByCurrentUser(filePath)) {
/* remove supporting database records, etc. here */
File.Delete(filePath);
}
but I have no idea how to implement FileIsDeletableByCurrentUser().
但我不知道如何实现 FileIsDeletableByCurrentUser()。
回答by Dale
Have you tried System.IO.File.GetAccessControl(filename)it should return a FileSecurity with information about the permissions for that file.
您是否尝试过System.IO.File.GetAccessControl(filename)它应该返回一个 FileSecurity,其中包含有关该文件权限的信息。
回答by peSHIr
Of course you can check ReadOnly flags using System.IO and probably ACL security on the file combined with the current user too, but like Mehrdad writes in his comment it would never be full-proof in all cases. So you would need exception handling for the exceptional case at all times (even if its just a top-level catch-all, that logs/shows an "unexpected problem" and kills your application).
当然,您可以使用 System.IO 来检查 ReadOnly 标志,也可以结合当前用户对文件的 ACL 安全性进行检查,但就像 Mehrdad 在他的评论中所写的那样,它永远不会在所有情况下都是完全证明。因此,您始终需要对异常情况进行异常处理(即使它只是一个顶级的 catch-all,它会记录/显示“意外问题”并杀死您的应用程序)。
回答by Jonas B
As stated above. Lookup the file permissions and compare with the user who is running the application.
如上所述。查找文件权限并与运行应用程序的用户进行比较。
You could always use this aproach as well
你也可以随时使用这种方法
bool deletemyfile()
{
try
{
...delete my file
return true;
}
catch
{
return false;
}
}
if it returns false you know it failed if it returns true then.. it worked and file is gone. Not sure what you're after exactly but this was the best I could think of
如果它返回 false 你就知道它失败了,如果它返回 true 那么..它工作了,文件消失了。不确定你到底在追求什么,但这是我能想到的最好的
回答by Tim Joseph
Strictly speaking, an UnauthorizedAccessException means that the path is a directory, so you can use a System.IO.Path.GetFileName(path) type command and catch the argument exception.
严格来说,UnauthorizedAccessException 意味着路径是一个目录,因此您可以使用 System.IO.Path.GetFileName(path) 类型的命令并捕获参数异常。
But if you want a more holistic solution, use System.IO.File.GetAccessControl as mentioned by Dale Halliwell
但是,如果您想要更全面的解决方案,请使用 Dale Halliwell 提到的 System.IO.File.GetAccessControl
回答by codymanix
You should get the access control list (ACL) of that file.
您应该获得该文件的访问控制列表 (ACL)。
But this doesn't necessarily mean you could actually delete it because the readonly flag could still be set or another program has locked the file.
但这并不一定意味着您实际上可以删除它,因为仍然可以设置 readonly 标志或另一个程序已锁定该文件。
回答by BBlake
Seems like it would be easier to do things in the order of:
似乎按照以下顺序做事情会更容易:
- Get whatever information you need about the file in order to do the other parts (delete database data, etc)
- Try to delete the file
- If you successfully delete the file, then carry out the rest of the "cleanup" work. If you don't successfully delete it, return/handle the exception, etc.
- 获取您需要的有关文件的任何信息以执行其他部分(删除数据库数据等)
- 尝试删除文件
- 如果您成功删除了该文件,则执行其余的“清理”工作。如果你没有成功删除它,返回/处理异常等。
回答by JaredPar
The problem with implementing FileIsDeletableByCurrentUser
is that it's not possible to do so. The reason is the file system is a constantly changing item.
实施的问题FileIsDeletableByCurrentUser
是不可能这样做。原因是文件系统是一个不断变化的项目。
In between any check you make to the file system and the next operation any number of events can and will happen. Including ...
在您对文件系统进行的任何检查和下一个操作之间,可能会发生任何数量的事件。包含 ...
- Permissions on the file could change
- The file could be deleted
- The file could be locked by another user / process
- The USB key the file is on could be removed
- 文件的权限可能会改变
- 该文件可以被删除
- 该文件可能被另一个用户/进程锁定
- 可以删除文件所在的 USB 密钥
The best function you could write would most aptly be named FileWasDeletableByCurrentUser
.
您可以编写的最佳函数最恰当地命名为FileWasDeletableByCurrentUser
.