how can you easily check if access is denied for a file in .NET?

后端 未结 6 476
无人及你
无人及你 2020-11-22 14:13

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 the

6条回答
  •  死守一世寂寞
    2020-11-22 14:52

    First, what Joel Coehoorn said.

    Also: you should examine the assumptions that underly your desire to avoid using try/catch unless you have to. The typical reason for avoiding logic that depends on exceptions (creating Exception objects performs poorly) probably isn't relevant to code that's opening a file.

    I suppose that if you're writing a method that populates a List by opening every file in a directory subtree and you expected large numbers of them to be inaccessible you might want to check file permissions before trying to open a file so that you didn't get too many exceptions. But you'd still handle the exception. Also, there's probably something terribly wrong with your program's design if you're writing a method that does this.

提交回复
热议问题