Enigma State Enigma State - 28 days ago 6
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...

Answer

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

string filenameWithoutPath = Path.GetFileName(filename);
Comments