user887983
user887983

Reputation:

get filenames from a directory without full path

Is there any function to get filenames without path?

I use

Directory.GetFiles(directory)

And then i replace directory with string.Empty for each filename, but a GetFilenamesWithoutFullPath would be nice.

Upvotes: 2

Views: 1173

Answers (2)

Egi
Egi

Reputation: 1256

Use lazyberezovsky's solution or use the FileInfo class that provides a "Name" property.

Upvotes: -1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

Use Path.GetFileName which returns file name and extension from full path

var files = Directory.GetFiles(directory).Select(f => Path.GetFileName(f));

Or even

var files = Directory.GetFiles(directory).Select(Path.GetFileName);

Upvotes: 6

Related Questions