Get only file names from a folder C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Get only file names from a folder C#

Hi everyone, I would like to get all files inside a specific folder. The method: Directory.GetFiles() returns the whole path, but I only want to get the file names. Alternatively I could use a way to split the file name from the path. But I don't know how to do that either. So if you could help me, I would really appreciate that.

4th Feb 2020, 5:06 PM
ExampleWasTaken
ExampleWasTaken - avatar
7 Answers
+ 1
You can try and use this: string name = Path.GetFileName(filename);
4th Feb 2020, 5:14 PM
Nill
Nill - avatar
+ 1
oh, right.. so you know the folder name?
4th Feb 2020, 5:16 PM
Nill
Nill - avatar
+ 1
split them by the separator \\ or \ or / then get the last index. filename = wholepath.split(Path.DirectorySeparatorChar.ToString()).Last()
4th Feb 2020, 5:18 PM
Taste
Taste - avatar
0
The problem is, I don't know the filenames.
4th Feb 2020, 5:15 PM
ExampleWasTaken
ExampleWasTaken - avatar
0
Yes, I know the complete path. Only the files inside the folder are unknown.
4th Feb 2020, 5:16 PM
ExampleWasTaken
ExampleWasTaken - avatar
0
thank you Taste! @Tim
4th Feb 2020, 5:18 PM
Nill
Nill - avatar
0
@Taste Once again, thank you very much for your support. Unfortunately it didn't work. It tells me that conversion from string to char isn't possible.
4th Feb 2020, 6:43 PM
ExampleWasTaken
ExampleWasTaken - avatar