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

 

正文

 #基础知识

   1、获得当前运行程序的路径

1 string rootPath = Directory.GetCurrentDirectory();

  2、获得该文件夹下的文件,返回类型为FileInfo

1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 FileInfo[] files=root.GetFiles();

  3、获得该文件夹下的子目录,返回类型为DirectoryInfo

1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 DirctoryInfo[] dics=root.GetDirectories();

  4、获得文件夹名

1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 string dicName=root.Name;

  5、获得文件夹完整的路径名

1 string path=@"X:\XXX\XX";
2 DirectoryInfo root = new DirectoryInfo(path);
3 string dicName=root.FullName;

  6、获取文件的Name和FullName

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

#只获取目录下一级的文件夹与文件

复制代码
 1 String path = @"X:\xxx\xxx";
 2 
 3 //第一种方法
 4 string[] files = Directory.GetFiles(path, "*.txt");
 5              
 6 foreach (string file in files)
 7 {
 8     Console.WriteLine(file);
 9 }
10  
11 //第二种方法
12 DirectoryInfo folder = new DirectoryInfo(path);
13             
14 foreach (FileInfo file in folder.GetFiles("*.txt"))
15 {
16     Console.WriteLine(file.FullName);
17 }
复制代码

# 递归地输出当前运行程序所在的磁盘下的所有文件名和子目录名

复制代码
 1         static void Main(string[] args)
 2         {
 3             //获取当前程序所在的文件路径
 4             String rootPath = Directory.GetCurrentDirectory();
 5             string parentPath = Directory.GetParent(rootPath).FullName;//上级目录
 6             string topPath = Directory.GetParent(parentPath).FullName;//上上级目录
 7             StreamWriter sw = null;
 8             try
 9             {
10                 //创建输出流,将得到文件名子目录名保存到txt中
11                 sw = new StreamWriter(new FileStream("fileList.txt", FileMode.Append));
12                 sw.WriteLine("根目录:" + topPath);
13                 getDirectory(sw, topPath, 2);
14             }
15             catch (IOException e)
16             {
17                 Console.WriteLine(e.Message);
18             }
19             finally
20             {
21                 if (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>
36         public static void getFileName(StreamWriter sw, string path, int indent)
37         {
38             DirectoryInfo root = new DirectoryInfo(path);
39             foreach (FileInfo f in root.GetFiles())
40             {
41                 for (int i = 0; i < indent; i++)
42                 {
43                     sw.Write("  ");
44                 }
45                 sw.WriteLine(f.Name);
46             }
47         }
48 
49         /// <summary>
50         /// 获得指定路径下所有子目录名
51         /// </summary>
52         /// <param name="sw">文件写入流</param>
53         /// <param name="path">文件夹路径</param>
54         /// <param name="indent">输出时的缩进量</param>
55         public static void getDirectory(StreamWriter sw, string path, int indent)
56         {
57             getFileName(sw, path, indent);
58             DirectoryInfo root = new DirectoryInfo(path);
59             foreach (DirectoryInfo d in root.GetDirectories())
60             {
61                 for (int i = 0; i < indent; i++)
62                 {
63                     sw.Write("  ");
64                 }
65                 sw.WriteLine("文件夹:" + d.Name);
66                 getDirectory(sw, d.FullName, indent + 2);
67                 sw.WriteLine();
68             }
69         }
复制代码

 

 

 

 

 

------------------------------------------------------


作者:willingtolove 

出处:http://www.cnblogs.com/willingtolove/ 

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 
分类:  C#
标签:  c#文件文件夹遍历
好文要顶  关注我  收藏该文   
3
1
 
 
 
« 上一篇: 【SqlServer】SqlServer中的计算列
» 下一篇: 全角的空格(A1A1)惹的祸!
posted @  2018-06-27 20:54 willingtolove 阅读(25571) 评论(0) 编辑 收藏

 

转载于:https://www.cnblogs.com/qqhfeng/p/10635937.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值