ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ListFiles

List/Get all files in a specified folder using LINQ. Doesn't include sub-directory files.

Source

using System.IO;

public static class MyExtensions
{
	public static List<string> ListFiles(this string folderPath)
	{
		if (!Directory.Exists(folderPath)) return null;
		return (from f in Directory.GetFiles(folderPath)
					select Path.GetFileName(f)).
		ToList();
	}
}

Example

path = @"C:\temp";
List<string> files = path.ListFiles();
if(files != null)
{
	foreach (var file in files)
	{
		Console.WriteLine(file);
	}               
}

Author: Earljon Hidalgo

Submitted on: 26 mrt. 2008

Language: C#

Type: System.Collections.Generic.List<T>

Views: 5306