在使用C#进行桌面应用开发中,经常会有对文件进行操作的情况,这时可能会需要对文件夹进行文件扫描,获取所有文件
做法如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/// <summary> /// 遍历获取所有文件 /// </summary> /// <param name="filePathByForeach"></param> /// <param name="result"></param> public static void ForeachFile( string filePathByForeach, ref string result) { try { DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //获取所在目录的文件夹 FileInfo[] file = theFolder.GetFiles(); //获取所在目录的文件 foreach (FileInfo fileItem in file) //遍历文件 { result += fileItem.DirectoryName + @"\" + fileItem.Name + "\n"; } //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { ForeachFile(NextFolder.FullName, ref result); } } catch (Exception) { throw ; } } |
以上就是c# 遍历获取所有文件的示例代码的详细内容,更多关于c# 遍历所有文件的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/xwcs/p/13508569.html