如何检索目录中的所有文件名? [英] How do I retrieve all filenames in a directory?

查看:144
本文介绍了如何检索目录中的所有文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检索与目录中的模式相匹配的所有文件名?我试过这个,但是它返回完整的路径而不是文件名。

How do I retrieve all filenames matching a pattern in a directory? I tried this but it returns the full path instead of the filename.

Directory.GetFiles (path, "*.txt")

我必须手动裁剪目录路径离开结果?这很容易,但也许有一个更简单的解决方案:)

Do I have to manually crop the directory path off of the result? It's easy but maybe there is an even simpler solution :)

推荐答案

foreach (string s in Directory.GetFiles(path, "*.txt").Select(Path.GetFileName))
       Console.WriteLine(s);

这篇关于如何检索目录中的所有文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆