【C#】C#获取文件夹下的所有文件

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

【C#】C#获取⽂件夹下的所有⽂件 #基础知识
1、获得当前运⾏程序的路径
1string rootPath = Directory.GetCurrentDirectory();
2、获得该⽂件夹下的⽂件,返回类型为FileInfo
1string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 FileInfo[] files=root.GetFiles();
3、获得该⽂件夹下的⼦⽬录,返回类型为DirectoryInfo
1string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 DirctoryInfo[] dics=root.GetDirectories();
4、获得⽂件夹名
1string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3string dicName=;
5、获得⽂件夹完整的路径名
1string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3string dicName=root.FullName;
6、获取⽂件的Name和FullName
1string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3foreach (FileInfo f in root.GetFiles())
4 {
5string name=;
6string fullName=f.FullName;
7 }
#只获取⽬录下⼀级的⽂件夹与⽂件
1 String path = @"X:\xxx\xxx";
2
3//第⼀种⽅法
4string[] files = Directory.GetFiles(path, "*.txt");
5
6foreach (string file in files)
7 {
8 Console.WriteLine(file);
9 }
10
11//第⼆种⽅法
12 DirectoryInfo folder = new DirectoryInfo(path);
13
14foreach (FileInfo file in folder.GetFiles("*.txt"))
15 {
16 Console.WriteLine(file.FullName);
17 }
# 递归地输出当前运⾏程序所在的磁盘下的所有⽂件名和⼦⽬录名
1static void Main(string[] args)
2 {
3//获取当前程序所在的⽂件路径
4 String rootPath = Directory.GetCurrentDirectory();
5string parentPath = Directory.GetParent(rootPath).FullName;//上级⽬录
6string topPath = Directory.GetParent(parentPath).FullName;//上上级⽬录
7 StreamWriter sw = null;
8try
9 {
10//创建输出流,将得到⽂件名⼦⽬录名保存到txt中
11 sw = new StreamWriter(new FileStream("fileList.txt", FileMode.Append));
12 sw.WriteLine("根⽬录:" + topPath);
13 getDirectory(sw, topPath, 2);
14 }
15catch (IOException e)
16 {
17 Console.WriteLine(e.Message);
18 }
19finally
20 {
21if (sw != null)
22 {
23 sw.Close();
24 Console.WriteLine("完成");
25 }
26 }
27
28 }
29
30///<summary>
31///获得指定路径下所有⽂件名
32///</summary>
33///<param name="sw">⽂件写⼊流</param>
34///<param name="path">⽂件写⼊流</param>
35///<param name="indent">输出时的缩进量</param>
36public static void getFileName(StreamWriter sw, string path, int indent)
37 {
38 DirectoryInfo root = new DirectoryInfo(path);
39foreach (FileInfo f in root.GetFiles())
40 {
41for (int i = 0; i < indent; i++)
42 {
43 sw.Write("");
44 }
45 sw.WriteLine();
46 }
47 }
48
49///<summary>
50///获得指定路径下所有⼦⽬录名
51///</summary>
52///<param name="sw">⽂件写⼊流</param>
53///<param name="path">⽂件夹路径</param>
54///<param name="indent">输出时的缩进量</param>
55public static void getDirectory(StreamWriter sw, string path, int indent)
56 {
57 getFileName(sw, path, indent);
58 DirectoryInfo root = new DirectoryInfo(path);
59foreach (DirectoryInfo d in root.GetDirectories())
60 {
61for (int i = 0; i < indent; i++)
62 {
63 sw.Write("");
64 }
65 sw.WriteLine("⽂件夹:" + );
66 getDirectory(sw, d.FullName, indent + 2);
67 sw.WriteLine();
68 }
69 }
------------------------------------------------------。

相关文档
最新文档