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

- 1 string path=@"X:\XXX\XX";
- 2 DirectoryInfo root = new DirectoryInfo(path);
- 3 foreach (FileInfo f in root.GetFiles())
- 4 {
- 5 string name=f.Name;
- 6 string fullName=f.FullName;
- 7 }

DirectoryInfo di = new DirectoryInfo(@"C:\Users\bt\Desktop\result");
FileInfo[] f = di.GetFiles("*.txt");//获取指定扩展名的文件
////没有xml文件
if (f.Length <= 0)
{
return;
}
////遍历所有文件(*.txtl),并打开读取文件内容
foreach (FileInfo myFile in f)
{
StreamReader sr = myFile.OpenText();
string sds = sr.ReadToEnd();
Console.WriteLine(sds);
}
Console.ReadKey();
////遍历所有文件夹
DirectoryInfo[] d = di.GetDirectories();
foreach (DirectoryInfo myDir in d)
{
string str = myDir.FullName;
Console.WriteLine(str);
}
Console.ReadKey();
#只获取目录下一级的文件夹与文件

- 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 }

C#获取文件夹下的所有文件的方法的更多相关文章
- java:多层文件夹情况下,判断文件夹下是否有文件夹,并获取到没有文件夹的名字的方法
业务问题案例 在公司遇到的一个问题,本以为很小很好解决,没想到花了一下午时间.图给的是文件路径,page1下有10个文件夹,每个有的有文件夹或者文件,要求得到page1下(即:123456789,10 ...
- JAVA获取文件夹下所有的文件
package com.test; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; im ...
- C++获取某个文件夹下的所有文件
获取某个文件夹下的所有文件,返回各文件的路径加文件名 path为某文件夹的路径:eg. char * filePath = "C:\\Users\\WUQP\\Desktop\\test_d ...
- C/C++ 获取文件夹下的所有文件列表
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51009608 提供一段C/C++代码示 ...
- GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级
一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...
- IO流的练习3 —— 复制多级文件夹下的指定文件并改名
需求:复制指定目录下的指定文件,并修改后缀名. 指定的文件是:.java文件. 指定的后缀名是:.jad 数据源所在文件夹:C:\Users\Administrator\Desktop\记录 目的地所 ...
- JAVA 遍历文件夹下的所有文件
JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...
- php 遍历一个文件夹下的所有文件和子文件
php 遍历一个文件夹下的所有文件和子文件 <?php /** * 将读取到的目录以数组的形式展现出来 * @return array * opendir() 函数打开一个目录句柄,可由 clo ...
- Java遍历一个文件夹下的全部文件
Java工具中为我们提供了一个用于管理文件系统的类,这个类就是File类,File类与其它流类不同的是,流类关心的是文件的内容.而File类关心的是磁盘上文件的存储. 一,File类有多个构造器,经常 ...
随机推荐
- Review of Semantic Segmentation with Deep Learning
In this post, I review the literature on semantic segmentation. Most research on semantic segmentati ...
- Shell字符串截取(非常详细)
假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符. 1 echo ${var#*//} 其中 var 是变量名,# 号是运算符, ...
- bash: ip: command not found
Centos安装 # yum install iproute2 iproute2-doc Ubuntu安装 # apt-get install iproute2 iproute2-doc
- LCA的几种做法
P3379 LCA $ 1:$蜗牛爬式 void dfs(int u,int fa) { f[u]=fa;//预处理father for(int i=head[u]; i; i=e[i].nxt) i ...
- RequireJS - 快速指南
原文: https://www.tutorialspoint.com/requirejs/requirejs_quick_guide.htm RequireJS - 概述 RequireJS是一个Ja ...
- VS2015 创建C++动态库及使用
转载:https://blog.csdn.net/w_x_myself/article/details/82252646 1.dll的特点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代 ...
- NLP基本模型
textcnn: 加载预训练词典:https://blog.csdn.net/nlpuser/article/details/83627709 构建textcnn网络:https://blog.csd ...
- asp.netcore 高并发下使用HttpClient的方法
大家都知道,使用HttpClient,在并发量不大的情况,一般没有任何问题:但是在并发量一上去,如果使用不当,会造成很严重的堵塞的情况. 解决方案如下: 一.可以参考微软官方提供的方法:https:/ ...
- 使用leaflet绘制geoJson中国边界
绘制中国边界 代码如下: function drawChina() { //设置样式 var myStyle = { "color": "#00f", &quo ...
- Ubuntu下配置Window CIFS共享
转自:https://blog.csdn.net/wanfengzhong/article/details/52550074 1. 准备windows共享文件夹 2. 安装 cifs-utilssud ...