如何轻松检查 .NET 中的文件是否被拒绝访问? [英] how can you easily check if access is denied for a file in .NET?

查看:30
本文介绍了如何轻松检查 .NET 中的文件是否被拒绝访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想在实际尝试打开文件之前检查我是否有权打开文件;除非必须,否则我不想使用 try/catch 进行此检查.是否有我可以事先检查的文件访问属性?

Basically, I would like to check if I have rights to open the file before I actually try to open it; I do not want to use a try/catch for this check unless I have to. Is there a file access property I can check before hand?

推荐答案

过去我已经做过无数次了,而且几乎每一次我都做错了尝试.

I have done this countless times in the past, and nearly every time I've done it I was wrong to even make the attempt.

文件权限(甚至文件存在)是易变的——它们可以随时更改.多亏了墨菲定律,这尤其包括您检查文件和尝试打开文件之间的短暂时间.还有其他原因可能会失败,例如锁定或网络可用性和路径解析.如果您所在的区域您知道需要先检查,则更有可能出现错误的结果.然而奇怪的是,它永远不会发生在您的测试或开发环境中,这些环境往往相当静态.这使得以后难以追查问题,并使此类错误更容易进入生产环境.

File permissions (even file existence) are volatile — they can change at any time. Thanks to Murphy's Law this especially includes the brief period between when you check the file and when you try to open it. There are other reasons this can fail, too, such as locking or network availability and path resolution. An errant result is even more likely if you're in an area where you know you need to check first. Yet strangely enough it will never happen in your testing or development environments, which tend to be fairly static. This makes the problem difficult to track down later and makes it easy for this kind of bug to make it into production.

这意味着,尽管进行了检查,但如果文件权限或存在性不佳,您仍必须准备好处理异常.异常处理代码是必需的,无论您是否提前检查文件,好的异常处理程序可以提供所有存在或权限检查的功能.

What this means is you must still be ready to handle the exception if file permissions or existence are bad, in spite of your check. Exception handling code is required, whether or not you check the file in advance, and good exception handler can provide all of the functionality of existence or permissions checks.

但是异常处理是不是很慢?我很高兴你问.是的,是的.事实上,展开堆栈以处理异常是您在单台计算机中可以做的最慢的事情.然而,重要的是要记住磁盘 I/O 甚至更慢——很多——并且调用 .Exists() 函数或检查权限总是会强制额外的 I/O 文件系统上的操作.

But isn't exception handling slow? I'm glad you asked. Yes, yes it is. In fact, unwinding the stack to handle an exception is up there with the slowest stuff you can do inside a single computer. However, it's important to remember that disk I/O is even slower — a lot slower — and calling the .Exists() function or checking permissions will always force an additional I/O operation on the file system.

因此,我们看到在尝试打开文件之前的初始检查既多余又浪费.与异常处理相比,没有额外的好处.它实际上会伤害而不是帮助你的表现.它增加了必须维护的更多代码的成本.最后,它会引入细微的错误.进行初步检查根本没有任何好处.

Therefore, we see an initial check before trying to open the file is both redundant and wasteful. There is no additional benefit over exception handling. It will actually hurt, not help, your performance. It adds cost in terms of more code that must be maintained. Finally, it can introduce subtle bugs. There is just no upside at all to doing the initial check.

相反,这里正确的做法是立即尝试打开文件,不进行初始检查,如果失败,则将精力放在一个好的异常处理程序上.无论您是在检查权限、锁定还是仅检查文件是否存在,都是如此.

Instead, the correct thing here is immediately trying to open the file, with no initial check, and putting your effort into a good exception handler if it fails. The same is true whether you're checking permissions, locking, or even just whether or not the file exists.

总而言之:选择是每次使用更多代码为文件检查支付额外成本,或者仅在某些时候以更少代码为异常处理支付较小但仍然很差的成本.

In summary: the choice is paying the extra cost for file check every time with more code, or paying the smaller-but-still-bad cost for exception handling only some of the time and with less code.

这篇关于如何轻松检查 .NET 中的文件是否被拒绝访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆