using System.IO; DirectoryInfo dirInfo = new DirectoryInfo(@"C:\temp"); FileInfo[] txtFiles = dirInfo.GetFiles().Where(f => f.Extension == ".txt").ToArray(); foreach (FileInfo file in txtFiles) { Console.WriteLine(file.Name); }In this example, we create a DirectoryInfo object for the directory "C:\temp" and then use the GetFiles method to retrieve an array of FileInfo objects for all the files in that directory. We then use the Where method to filter the list of files to only those with a .txt extension, and we convert that filtered list to an array. Finally, we loop through the filtered array of FileInfo objects and write out the name of each file to the console. This code example uses the .NET Framework's System.IO namespace.