相关文章推荐
性感的鸵鸟  ·  curl命令 CURL命令 测试 ...·  2 月前    · 
八块腹肌的葡萄酒  ·  Laravel7 - 写单元测试时,提示 ...·  1 月前    · 
豪情万千的双杠  ·  解决 Laravel 单元测试报错:A ...·  1 月前    · 
奔跑的黄瓜  ·  MySQL:BOOLEAN(又名tinyin ...·  10 月前    · 
销魂的大白菜  ·  ssh连接远程主机执行脚本的环境变量问题 - 简书·  1 年前    · 
精明的针织衫  ·  js ...·  1 年前    · 
眼睛小的大白菜  ·  Matlab用三种格式来表示日期与时间_ma ...·  1 年前    · 
坚强的香蕉  ·  Django ORM Queries - ...·  1 年前    · 
Code  ›  C#获取文件夹下的所有文件开发者社区
string root
https://cloud.tencent.com/developer/article/1895670
求醉的大熊猫
1 年前
作者头像
用户9127601
0 篇文章

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

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > dotNET编程大全 > C#获取文件夹下的所有文件

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

作者头像
用户9127601
发布 于 2021-11-01 17:06:15
3.9K 0
发布 于 2021-11-01 17:06:15
举报

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

 string rootPath = Directory.GetCurrentDirectory();

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

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

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

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

4、获得文件夹名

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

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

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

6、获取文件的Name和FullName


 string path=@"X:\XXX\XX";
 DirectoryInfo root = new DirectoryInfo(path);
foreach (FileInfo f in root.GetFiles())
    string name=f.Name;
    string fullName=f.FullName;
} 

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


 String path = @"X:\xxx\xxx";
 //第一种方法
  string[] files = Directory.GetFiles(path, "*.txt");
 foreach (string file in files)
      Console.WriteLine(file);
 //第二种方法
 DirectoryInfo folder = new DirectoryInfo(path);
 foreach (FileInfo file in folder.GetFiles("*.txt"))
     Console.WriteLine(file.FullName);
 }

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

 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             }
28         }
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         }
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("  ");
 
推荐文章
性感的鸵鸟  ·  curl命令 CURL命令 测试 http接口 测试api_curl keystore
2 月前
八块腹肌的葡萄酒  ·  Laravel7 - 写单元测试时,提示 A facade root has not been set。
1 月前
豪情万千的双杠  ·  解决 Laravel 单元测试报错:A facade root has not been set开发者社区
1 月前
奔跑的黄瓜  ·  MySQL:BOOLEAN(又名tinyint(1))vs BIT - 腾讯云开发者社区 - 腾讯云
10 月前
销魂的大白菜  ·  ssh连接远程主机执行脚本的环境变量问题 - 简书
1 年前
精明的针织衫  ·  js 获取浏览器高度和宽度值(多浏览器)-腾讯云开发者社区-腾讯云
1 年前
眼睛小的大白菜  ·  Matlab用三种格式来表示日期与时间_matlab 默认的时间格式_子衿_青青的博客-CSDN博客
1 年前
坚强的香蕉  ·  Django ORM Queries - javatpoint
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号