鱼C论坛

 找回密码
 立即注册
查看: 2757|回复: 4

求遍历文件夹下面所有的子文件夹和文件名

[复制链接]
发表于 2020-5-18 17:40:22 | 显示全部楼层 |阅读模式
60鱼币
开始学习C#了...第一个作业我就不会做..求助了悬赏了..想用C# 写一个class 类. 遍历文件夹下面所有的子文件夹和文件夹里面所有文件的名字.直到子文件夹下面的最底层=.=求助 求助

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-20 07:31:35 From FishC Mobile | 显示全部楼层
这里几乎没有玩c井的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-20 10:30:54 | 显示全部楼层
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;

  7. namespace ConsoleApp1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //获取当前程序所在的文件路径
  14.             String path = Directory.GetCurrentDirectory();
  15.             String path2 = path.Substring(0, 3);     //取盘符
  16.             StreamWriter sw = null;
  17.             try
  18.             {
  19.                 //创建输出流,将得到文件名子目录名保存到txt中
  20.                 sw = new StreamWriter(new FileStream("files.txt", FileMode.Append));
  21.                 sw.WriteLine("根目录:" + path2);
  22.                 getDirectory(sw, path2, 2);
  23.             }
  24.             catch (IOException e)
  25.             {
  26.                 Console.WriteLine(e.Message);
  27.             }
  28.             finally
  29.             {
  30.                 if (sw != null)
  31.                 {
  32.                     sw.Close();
  33.                     Console.WriteLine("完成");
  34.                 }
  35.             }


  36.         }

  37.         /*
  38.          * 获得指定路径下所有文件名
  39.          * StreamWriter sw  文件写入流
  40.          * string path      文件路径
  41.          * int indent       输出时的缩进量
  42.          */
  43.         public static void getFileName(StreamWriter sw, string path, int indent)
  44.         {
  45.             DirectoryInfo root = new DirectoryInfo(path);
  46.             foreach (FileInfo f in root.GetFiles())
  47.             {
  48.                 for (int i = 0; i < indent; i++)
  49.                 {
  50.                     sw.Write("  ");
  51.                 }
  52.                 sw.WriteLine(f.Name);
  53.             }
  54.         }

  55.         //获得指定路径下所有子目录名
  56.         public static void getDirectory(StreamWriter sw, string path, int indent)
  57.         {
  58.             getFileName(sw, path, indent);
  59.             DirectoryInfo root = new DirectoryInfo(path);
  60.             foreach (DirectoryInfo d in root.GetDirectories())
  61.             {
  62.                 for (int i = 0; i < indent; i++)
  63.                 {
  64.                     sw.Write("  ");
  65.                 }
  66.                 sw.WriteLine("文件夹:" + d.Name);
  67.                 getDirectory(sw, d.FullName, indent + 2);
  68.                 sw.WriteLine();
  69.             }
  70.         }
  71.     }
  72. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-20 15:03:47 | 显示全部楼层

想要写在class 的,可以直接调用的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-20 15:14:27 | 显示全部楼层
有了代码,你自己去建一个就可以了撒,这个还要调试一下的·· 要不我帮你把整个程序一起写了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 21:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表