c# Directory.GetDirectories excluding folders

本秂侑毒 提交于 2019-12-06 00:17:06

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

You could do:

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

Directory.GetDirectories() returns the full path of the directory, not just the last part of the directory.

While you COULD strip off the last component of the path returned by GetDirectories() and compare that to what's currently in your array, that will result in false positives and false negatives.

Instead, use Environment.SpecialFolders to get the path for a given special folder specific to the current user and operating system version.

private readonly List<String> _exclusion = new List<String>
{
    Environment.GetFolderPath(Environment.SpecialFolder.MyMusic),
    // etc.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!