How do I retrieve all filenames in a directory?

2020-06-08 13:14发布

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 :)

7条回答
够拽才男人
2楼-- · 2020-06-08 14:13

Do you want to recurse through subdirectories? Use Directory.EnumerateFiles:

var fileNames = Directory.EnumerateFiles(@"\", "*.*", SearchOption.AllDirectories);
查看更多
登录 后发表回答