简体   繁体   中英

Find all files in first sub directories

I have an application which searches in all directories behind Documents/GameLauncher/ Like this:

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

This works fine but now I only want to find all the applications in the first sub directories of this folder. Like this:

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

I have searched around and came up with this:

//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());
}

Does anyone have any tips to solve this problem or even a better way to find those files in the first sub directories of my GameLauncher folder?

Thanks for reading/helping.

Just don't use the "all" option if you don't want all, simple as that.

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;

You should be working with a list instead of an IEnumerable since it will grow dynamically.

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());
}

If you want to append one IEnumerable to another you need to use Concat . You'll also have to initialize foundApplications to an empty 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());
}

Also I'm pretty sure you want to use SearchOption.TopDirectoryOnly not SearchOption.AllDirectories

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM