如何在没有尝试的情况下检查File.Delete()是否会成功,在C#中?

时间:2022-04-13 20:17:50

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()。

8 个解决方案

#1


11  

Take a look at this article - http://www.codeproject.com/KB/files/UserFileAccessRights.aspx

看一下这篇文章 - http://www.codeproject.com/KB/files/UserFileAccessRights.aspx

#2


16  

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。

#3


8  

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,其中包含有关该文件权限的信息。

#4


2  

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

#5


1  

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然后..它工作,文件消失了。不确定你到底是怎么回事,但这是我能想到的最好的

#6


0  

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在他的评论中写道,它在所有情况下都不会是完全证明的。因此,您需要始终对异常情况进行异常处理(即使它只是一个*的捕获,记录/显示“意外问题”并杀死您的应用程序)。

#7


0  

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.

但这并不一定意味着您实际上可以删除它,因为仍然可以设置只读标志或其他程序已锁定文件。

#8


0  

Seems like it would be easier to do things in the order of:

似乎按以下顺序执行操作会更容易:

  1. Get whatever information you need about the file in order to do the other parts (delete database data, etc)
  2. 获取有关该文件的任何信息,以便执行其他部分(删除数据库数据等)
  3. Try to delete the file
  4. 尝试删除该文件
  5. 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.
  6. 如果您成功删除了该文件,则执行剩余的“清理”工作。如果您没有成功删除它,请返回/处理异常等。

#1


11  

Take a look at this article - http://www.codeproject.com/KB/files/UserFileAccessRights.aspx

看一下这篇文章 - http://www.codeproject.com/KB/files/UserFileAccessRights.aspx

#2


16  

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。

#3


8  

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,其中包含有关该文件权限的信息。

#4


2  

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

#5


1  

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然后..它工作,文件消失了。不确定你到底是怎么回事,但这是我能想到的最好的

#6


0  

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在他的评论中写道,它在所有情况下都不会是完全证明的。因此,您需要始终对异常情况进行异常处理(即使它只是一个*的捕获,记录/显示“意外问题”并杀死您的应用程序)。

#7


0  

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.

但这并不一定意味着您实际上可以删除它,因为仍然可以设置只读标志或其他程序已锁定文件。

#8


0  

Seems like it would be easier to do things in the order of:

似乎按以下顺序执行操作会更容易:

  1. Get whatever information you need about the file in order to do the other parts (delete database data, etc)
  2. 获取有关该文件的任何信息,以便执行其他部分(删除数据库数据等)
  3. Try to delete the file
  4. 尝试删除该文件
  5. 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.
  6. 如果您成功删除了该文件,则执行剩余的“清理”工作。如果您没有成功删除它,请返回/处理异常等。