查找第一个子目录中的所有文件

Find all files in first sub directories

我有一个应用程序可以在 Documents/GameLauncher/ 后面的所有目录中搜索,如下所示:

var foundApplications = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/GameLauncher", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".exe") || s.EndsWith(".lnk") || s.EndsWith(".url"));

这很好用,但现在我只想在该文件夹的第一个子目录中找到所有应用程序。像这样:

GameLauncher/test/test.exe <--- find this file
GameLauncher/test/test/test.exe <--- Ignore this file
GameLauncher/hello/hello.exe <--- find this file

我四处搜索并想出了这个:

//Search for first sub directories of path
var folders = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/GameLauncher");
IEnumerable<string> foundApplications;

//Use folders to find applications and add them to foundApplications
for (int i = 0; i < folders.Count(); i++)
{
    foundApplications += Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/GameLauncher/" + folders[i], "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".exe") || s.EndsWith(".lnk") || s.EndsWith(".url"));
}

//Ends up with error "Use of unassigned local variable 'foundApplications'" when using = instead of += in the forloop above.
foreach (var application in foundApplications){
    MessageBox.Show(application.ToString());
}

有没有人有解决此问题的任何提示,或者有更好的方法可以在我的 GameLauncher 文件夹的第一个子目录中找到这些文件?

感谢reading/helping。

您应该使用列表而不是 IEnumerable,因为它会动态增长。

var foundApplications = new List<string>();

var folders = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/GameLauncher");

//Use folders to find applications and add them to foundApplications
for (int i = 0; i < folders.Count(); i++)
{
    foundApplications.AddRange(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/GameLauncher/" + folders[i], "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".exe") || s.EndsWith(".lnk") || s.EndsWith(".url").ToList());
}

foreach (var application in foundApplications){
    MessageBox.Show(application.ToString());
}

如果您不想要全部,就不要使用 "all" 选项,就这么简单。

var path = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    @"GameLauncher");
var includedExtensions = new HashSet<string> { ".exe", ".lnk", ".url" };
var files =
    from dir in Directory.EnumerateDirectories(path)
    from file in Directory.EnumerateFiles(dir)
    let extension = Path.GetExtension(file)
    where includedExtensions.Contains(extension)
    select file;

如果您想将一个 IEnumerable 附加到另一个,您需要使用 Concat。您还必须将 foundApplications 初始化为空 IEnumerable.

var folderPath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), 
    "GameLauncher");
var folders = Directory.GetDirectories(folderPath);
IEnumerable<string> foundApplications = Enumerable<string>.Empty;

//Use folders to find applications and add them to foundApplications
foreach(var subFolder in folders)
{
    string path = Path.Combine(folderPath, subFolder);
    foundApplications.Concat(
        Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly)
            .Where(s => s.EndsWith(".exe") || s.EndsWith(".lnk") || s.EndsWith(".url")));
}

foreach (var application in foundApplications){
    MessageBox.Show(application.ToString());
}

另外我很确定你想使用 SearchOption.TopDirectoryOnly 而不是 SearchOption.AllDirectories