Wednesday, June 17, 2026

C# Filter files - Get a list of text files in a folder

The following LINQ technique simplifies the code to filter files in a folder.
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source_folder = Path.Combine(desktop, "source_folder");

if (Directory.Exists(source_folder))
{
    Directory.GetFiles(source_folder).ToList()
        .Where(file => file.EndsWith(".txt")).ToList()
        .ForEach(x => Console.WriteLine(x));
}

No comments:

Post a Comment

Hot Topics