33

I am using the below method to get the file names. But it returns the entire path and I don't 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) { } 

5 Answers 5

54

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

string filenameWithoutPath = Path.GetFileName(filename); 
Sign up to request clarification or add additional context in comments.

1 Comment

I have lot of files with same type how can i get list of files that filenames only contains that file name
12

System.IO.Path is your friend here:

var filenames = from fullFilename in Directory.EnumerateFiles(targetdirectory,"backup-*.zip") select Path.GetFileName(fullFilename); foreach (string filename in filenames) { // ... } 

1 Comment

what i have to put in this place fullFilename do i have to give
3

Try GetFileName() method:

Path.GetFileName(filename); 

Comments

2

Linq is good

Directory.GetFiles( dir ).Select( f => Path.GetFileName( f ) ).ToArray();

Comments

1
You can use this, it will give you all file's name without Extension List<string> lstAllFileName = (from itemFile in dir.GetFiles() select Path.GetFileNameWithoutExtension(itemFile.FullName)).Cast<string>().ToList(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.