简体   繁体   English

从没有完整路径的目录中获取文件名

[英]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. 然后我用每个文件名的string.Empty替换directory ,但是GetFilenamesWithoutFullPath会很好。

Use Path.GetFileName which returns file name and extension from full path 使用Path.GetFileName从完整路径返回文件名和扩展名

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

Or even 甚至

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

使用lazyberezovsky的解决方案或使用提供“名称”属性的FileInfo类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM