Possible Duplicate:
How to get only filenames within a directory using c#?
Using C#, I want to get the list of files in a folder.
My goal: ["file1.txt", "file2.txt"]
So I wrote this:
string[] files = Directory.GetFiles(dir);
Unfortunately, I get this output: ["C:\\dir\\file1.txt", "C:\\dir\\file2.txt"]
I could strip the unwanted "C:\dir\" part afterward, but is there a more elegant solution?
string[] files = Directory.GetFiles(dir).Select(file => Path.GetFileName(file)).ToArray();
– Cartie