[英]How can i ignore access denied when searching to get all sub directories?
static IEnumerable<string> GetSubdirectoriesContainingOnlyFiles(string path)
{
try
{
return from subdirectory in Directory.GetDirectories(path, "*", SearchOption.AllDirectories)
where Directory.GetDirectories(subdirectory).Length == 0
select subdirectory;
}
catch
{
}
}
In this case i'm searching in c:\\ So some directories are access denied. 在这种情况下,我正在c:\\中搜索,因此某些目录被拒绝访问。 I added try and catch but now this method dosent have a return.
我添加了try and catch,但是现在此方法返回了。
And how or should i handle at all it when it's getting to the catch ? 而且,当问题到来时,我应该怎么处理呢? I want in the end to get a List of all sub directories so i can get all sub directories names and the Length(the number of sub directories).
我想最后获得所有子目录的列表,以便我可以获取所有子目录的名称和Length(子目录的数量)。
UPDATE 更新
I tried this in the class constructor: 我在类构造函数中尝试过:
if (m_pars.SearchDir != null)
{
ApplyAllFiles(m_pars.SearchDir,ProcessFile);
}
m_pars.SearchDir in this contain C:\\ m_pars.SearchDir在此包含C:\\
Then in ApplyAllFiles: 然后在ApplyAllFiles中:
static List<string> allsubdirs = new List<string>();
static void ProcessFile(string path) {/* ... */}
public static void ApplyAllFiles(string folder, Action<string> fileAction)
{
foreach (string file in Directory.GetFiles(folder))
{
fileAction(file);
}
foreach (string subDir in Directory.GetDirectories(folder))
{
try
{
ApplyAllFiles(subDir, fileAction);
allsubdirs.Add(subDir);
}
catch
{
// swallow, log, whatever
}
}
}
But the List allsubdirs is empty. 但是List allsubdirs为空。
Your problem might be that you don't visit (add to the list) the current directory before recursively visiting its subdirectories. 您的问题可能是您递归访问子目录之前没有访问(添加到列表)当前目录。 So if you get an exception there, nothing will be added to the list.
因此,如果在那里出现异常,则不会将任何内容添加到列表中。
The following works for me. 以下对我有用。 (I've also made it a bit more generic by using callbacks and made the exception handling stricter.)
(我还通过使用回调使它更加通用,并使异常处理更加严格。)
class DirectoryHelper
{
public static void Test()
{
DirectoryHelper.EnumerateSubDirectories(@"c:\windows\system32");
}
public static List<string> EnumerateSubDirectories(string path)
{
// Depending on your use case, it might be
// unecessary to save these in memory
List<string> allSubdirs = new List<string>();
EnumerateSubDirectories(path,
filePath => Console.WriteLine("Visited file: " + filePath),
dirPath => allSubdirs.Add(dirPath),
noAccessPath => Console.WriteLine("No access: " + noAccessPath)
);
return allSubdirs;
}
private static void EnumerateSubDirectories(string root, Action<string> fileAction, Action<string> subdirAction, Action<string> noAccessAction)
{
foreach (string file in Directory.GetFiles(root))
{
fileAction(file);
}
foreach (string dir in Directory.GetDirectories(root))
{
try
{
subdirAction(dir);
EnumerateSubDirectories(dir, fileAction, subdirAction, noAccessAction);
}
catch (UnauthorizedAccessException)
{
noAccessAction(dir);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.