本文介绍了C#Directory.GetDirectories排除文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图通过Windows用户文件夹列表迭代在C:\Users,但不包括微软内置的用户文件夹,下面是我使用完成这一壮举的代码段,但。这是因为某些原因不能如预期运行

I'm trying to iterate through a list of users folders in windows in "c:\Users" but exclude the microsoft built-in user folders, the below is the code segment I'm using to accomplish this feat but it's for some reason not working as intended.

private readonly List<String> _exclusion = new List<String>
                                                   {
                                                       "All Users",
                                                       "Default",
                                                       "LocalService",
                                                       "Public",
                                                       "Administrator",
                                                       "Default User",
                                                       "NetworkService"
                                                   };

public static bool FoundInArray(List<string> arr, string target)
{
    return arr.Exists(p => p.Trim() == target);
}

foreach (string d in Directory.GetDirectories(sDir).Where(d => !FoundInArray(_exclusion,d)))
{
    richTextBox1.Text += d + Environment.Newline;
}



我不知道为什么,这是行不通的,任何人都可以提供一些?在这种洞察力我

I'm not sure why this isn't working, can anyone provide some insight on this for me?

推荐答案

在您的lambda表达式:D是目录的全名(与路径),因此,实际上不是数组中。

In your lambda expression: 'd' is the full name of the directory (with the path), and therefore is not actually in the array.

您可以这样做:

public static bool FoundInArray(List<string> arr, string target)
{
    return arr.Any(p => new DirectoryInfo(target).Name == p);
}

这篇关于C#Directory.GetDirectories排除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:12