Enigma State Enigma State - 5 months ago 30
C# Question

how to get only file names from the directory ,not the entire path

i am using below method to get the file names ..

but it returns the entire path ....now i dont want to get the entire path..

I want only file names not the entire path...

how can i get that only file names not the entire path

path= c:\docs\doc\backup-23444444.zip

string[] filenames = Directory.GetFiles(targetdirectory,"backup-*.zip");
foreach (string filename in filenames)
{ }

would any one pls help on this.....

Many thanks...


You could use the GetFileName method to extract only the filename without a path:

string filenameWithoutPath = Path.GetFileName(filename);