.NET - Check if directory is accessible without exception handling

后端 未结 3 582
自闭症患者
自闭症患者 2020-12-10 12:27

I need to go through various directories on the computer (via DirectoryInfo). Some of them aren\'t accessible, and UnauthorizedAccessException occurs. How can I check direct

3条回答
  •  孤城傲影
    2020-12-10 13:28

    Simply put you can't. There is no way to check if a directory is accessible, all you can determine is that it was accessible. The reason why is as soon as the check completes the permissions can changed and invalidate your check. The most reliable strategy you can implement is to access the directories and catch the UnauthorizedAccessException.

    I wrote a blog article on this subject recently which goes into a bit of detail here

    • http://blogs.msdn.com/jaredpar/archive/2009/12/10/the-file-system-is-unpredictable.aspx

提交回复
热议问题