目录

正文

 

#基础知识

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

  1. 1 string rootPath = Directory.GetCurrentDirectory();

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

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

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

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

  4、获得文件夹名

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

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

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

  6、获取文件的Name和FullName

  1. 1 string path=@"X:\XXX\XX";
  2. 2 DirectoryInfo root = new DirectoryInfo(path);
  3. 3 foreach (FileInfo f in root.GetFiles())
  4. 4 {
  5. 5 string name=f.Name;
  6. 6 string fullName=f.FullName;
  7. 7 }
  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. 1 String path = @"X:\xxx\xxx";
  2. 2
  3. 3 //第一种方法
  4. 4 string[] files = Directory.GetFiles(path, "*.txt");
  5. 5
  6. 6 foreach (string file in files)
  7. 7 {
  8. 8 Console.WriteLine(file);
  9. 9 }
  10. 10
  11. 11 //第二种方法
  12. 12 DirectoryInfo folder = new DirectoryInfo(path);
  13. 13
  14. 14 foreach (FileInfo file in folder.GetFiles("*.txt"))
  15. 15 {
  16. 16 Console.WriteLine(file.FullName);
  17. 17 }

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

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

C#获取文件夹下的所有文件的方法的更多相关文章

  1. java:多层文件夹情况下,判断文件夹下是否有文件夹,并获取到没有文件夹的名字的方法

    业务问题案例 在公司遇到的一个问题,本以为很小很好解决,没想到花了一下午时间.图给的是文件路径,page1下有10个文件夹,每个有的有文件夹或者文件,要求得到page1下(即:123456789,10 ...

  2. JAVA获取文件夹下所有的文件

    package com.test; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; im ...

  3. C++获取某个文件夹下的所有文件

    获取某个文件夹下的所有文件,返回各文件的路径加文件名 path为某文件夹的路径:eg. char * filePath = "C:\\Users\\WUQP\\Desktop\\test_d ...

  4. C/C++ 获取文件夹下的所有文件列表

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51009608 提供一段C/C++代码示 ...

  5. GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级

    一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...

  6. IO流的练习3 —— 复制多级文件夹下的指定文件并改名

    需求:复制指定目录下的指定文件,并修改后缀名. 指定的文件是:.java文件. 指定的后缀名是:.jad 数据源所在文件夹:C:\Users\Administrator\Desktop\记录 目的地所 ...

  7. JAVA 遍历文件夹下的所有文件

    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...

  8. JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)

    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...

  9. php 遍历一个文件夹下的所有文件和子文件

    php 遍历一个文件夹下的所有文件和子文件 <?php /** * 将读取到的目录以数组的形式展现出来 * @return array * opendir() 函数打开一个目录句柄,可由 clo ...

  10. Java遍历一个文件夹下的全部文件

    Java工具中为我们提供了一个用于管理文件系统的类,这个类就是File类,File类与其它流类不同的是,流类关心的是文件的内容.而File类关心的是磁盘上文件的存储. 一,File类有多个构造器,经常 ...

随机推荐

  1. Review of Semantic Segmentation with Deep Learning

    In this post, I review the literature on semantic segmentation. Most research on semantic segmentati ...

  2. Shell字符串截取(非常详细)

    假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符. 1 echo ${var#*//} 其中 var 是变量名,# 号是运算符, ...

  3. bash: ip: command not found

    Centos安装 # yum install iproute2 iproute2-doc Ubuntu安装 # apt-get install iproute2 iproute2-doc

  4. 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 ...

  5. RequireJS - 快速指南

    原文: https://www.tutorialspoint.com/requirejs/requirejs_quick_guide.htm RequireJS - 概述 RequireJS是一个Ja ...

  6. VS2015 创建C++动态库及使用

    转载:https://blog.csdn.net/w_x_myself/article/details/82252646 1.dll的特点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代 ...

  7. NLP基本模型

    textcnn: 加载预训练词典:https://blog.csdn.net/nlpuser/article/details/83627709 构建textcnn网络:https://blog.csd ...

  8. asp.netcore 高并发下使用HttpClient的方法

    大家都知道,使用HttpClient,在并发量不大的情况,一般没有任何问题:但是在并发量一上去,如果使用不当,会造成很严重的堵塞的情况. 解决方案如下: 一.可以参考微软官方提供的方法:https:/ ...

  9. 使用leaflet绘制geoJson中国边界

    绘制中国边界 代码如下: function drawChina() { //设置样式 var myStyle = { "color": "#00f", &quo ...

  10. Ubuntu下配置Window CIFS共享

    转自:https://blog.csdn.net/wanfengzhong/article/details/52550074 1. 准备windows共享文件夹 2. 安装 cifs-utilssud ...