如何遍历C#.net目录中的所有文件? [英] How to loop through all the files in a directory in c # .net?

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

问题描述

我要在文件夹及其子文件夹中获取文件。以下代码不在其子文件夹中获取文件:

I want to get the files in folder and also in its subfolders.The following code does not get the files in its subfolder:

string[] files = Directory.GetFiles(txtFolderPath.Text, "*ProfileHandler.cs");

有人可以告诉我如何在c#.net中实现吗?

Can anyone Please tell me how to implement this in c# .net?

推荐答案

string[] files = 
    Directory.GetFiles(txtPath.Text, "*ProfileHandler.cs", SearchOption.AllDirectories);

最后一个参数完全影响您所指的内容。对于每个文件(包括子文件夹),将其设置为 AllDirectories ;如果只想在给定目录而不是子文件夹中搜索,则将其设置为 TopDirectoryOnly

That last parameter effects exactly what you're referring to. Set it to AllDirectories for every file including in subfolders, and set it to TopDirectoryOnly if you only want to search in the directory given and not subfolders.

有关详细信息,请参阅MDSN: https://msdn.microsoft.com/zh-CN/library/ms143316(v = vs.110).aspx

Refer to MDSN for details: https://msdn.microsoft.com/en-us/library/ms143316(v=vs.110).aspx

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

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