c# – 使用getDirectories的UnauthorizedAccessException
发布时间:2020-12-15 21:00:02 所属栏目:百科 来源:网络整理
导读:大家好,我目前通过此次调用获得了我想要的子目录: foreach (DirectoryInfo dir in parent) { try { subDirectories = dir.GetDirectories().Where(d = d.Exists == true).ToArray(); } catch(UnauthorizedAccessException e) { Console.WriteLine(e.Message
大家好,我目前通过此次调用获得了我想要的子目录:
foreach (DirectoryInfo dir in parent) { try { subDirectories = dir.GetDirectories().Where(d => d.Exists == true).ToArray(); } catch(UnauthorizedAccessException e) { Console.WriteLine(e.Message); } foreach (DirectoryInfo subdir in subDirectories) { Console.WriteLine(subdir); var temp = new List<DirectoryInfo>(); temp = subdir.GetDirectories("*",SearchOption.AllDirectories).Where(d => reg.IsMatch(d.Name)).Where((d => !d.FullName.EndsWith("TESTS"))).Where(d => !(d.GetDirectories().Length == 0 && d.GetFiles().Length == 0)).Where(d => d.GetFiles().Length > 3).ToList(); candidates.AddRange(temp); } } foreach(DirectoryInfo dir in candidates) { Console.WriteLine(dir); } 所以现在我的问题是我的最终列表称为候选人我什么都没得到,因为我得到一个访问问题,因为在try块中我的子目录文件夹中找到了一个名为lost的文件夹.我尝试使用try和catch来处理异常,所以我可以继续做我的检查我实际上不关心这个文件夹,我试图忽略它但我不知道怎么去忽略它我的get目录搜索任何想法?我已经尝试使用.where来做一个过滤器来忽略任何包含文件夹名称的文件夹但是它没有用,它只是在文件夹名称中停止了我的程序. 解决方法
您可以像Microsoft解释的那样使用递归:
link.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |