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

 

 

 

 

 

——————————————————

版权声明:本文为willingtolove原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/willingtolove/p/9235353.html