Posted By: Anonymous
How to get only filenames within a directory using c#?
Using C#, I want to get the list of files in a folder.
So I wrote this:
string files = Directory.GetFiles(dir);
Unfortunately, I get this output:
I could strip the unwanted “C:dir” part afterward
You can use
System.IO.Path.GetFileName to do this.
string files = Directory.GetFiles(dir); foreach(string file in files) Console.WriteLine(Path.GetFileName(file));
While you could use
FileInfo, it is much more heavyweight than the approach you are already using (just retrieving file paths). So I would suggest you stick with
GetFiles unless you need the additional functionality of the