c# get file from directory code example

Example 1: get directory of file c#

using System.IO;

string file = "C:\Documents\file.txt";
Path.GetDirectoryName(file);

Example 2: c# retrieve files in folder

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp");
// returns:
// "c:\MyDir\my-car.BMP"

Example 3: c# retrieve files in folder

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp",
                                         SearchOption.AllDirectories);
// returns:
// "c:\MyDir\my-car.BMP"
// "c:\MyDir\Friends\james.BMP"