Enumerating files in a directory in C#
Posted: (EET/GMT+2)
Working with files and directories is easy with C#, I've found. For instance, if you need to list files in a directory, you can use the built-in System.IO.Directory methods.
As an example, the GetFiles method returns all files in a directory as a string array:
using System.IO;
...
string[] files = Directory.GetFiles(@"C:\Temp");
foreach (string file in files)
{
Console.WriteLine(file);
}
This will print the full paths and full filenames of each matching file. If you want to filter by extension, you can use a search pattern:
string[] files = Directory.GetFiles(@"C:\Temp", "*.txt");
Hope this helps!