开发者

trying to use Directory .EnumerateFiles("*.jpg", SearchOption.AllDirectories); [duplicate]

开发者 https://www.devze.com 2023-03-09 06:39 出处:网络
This question already has answers here: UnauthorizedAccessException cannot resolve Directory.GetFiles failure [duplicate]
This question already has answers here: UnauthorizedAccessException cannot resolve Directory.GetFiles failure [duplicate] (6 answers) Closed 7 years ago.

So i'm using Directory.GetFiles(folder, "*.jpg", SearchOption.AllDirectories). When i try it on the c:\ drive in windows 7, since windows 7 still had c:\documents and settings (for legacy systems like xp), it's a protected folder. It's only protected because it's not really a folder. It's kind of a dummy folder. So when I run this code:

try
{
    string[] folders = Directory.GetFiles(@"C:\\","*.jpg", SearchOption.AllDirectories);
    foreach (string item in folders)
    { 
       //blah blah
    }
catch
{
}

It always throws an UnauthorizedAccessException. It doesn't let me past that. What I'm trying to do is have it skip over any folder that it doesn't have permis开发者_如何学Pythonsion to access, but just continue to search, don't get caught up on the catch block, just continue. Any ideas?


I would try one of the two workarounds in this article (click on workarounds tab near the bottom):

http://connect.microsoft.com/VisualStudio/feedback/details/94751/optionally-prevent-directoryinfo-getfiles-from-throwing-unauthorizedaccessexception

0

精彩评论

暂无评论...
验证码 换一张
取 消