zoukankan      html  css  js  c++  java
  • C#遍历指定文件夹下的所有文件和所有子目录

    程序使用Directory、DirectoryInfo类

    1. Directory.GetCurrentDirectory()获得当前运行程序的路径
    2. DirectoryInfo对象.GetDirectories()获得该文件夹下的子目录,返回类型为DirectoryInfo
    3. DirectoryInfo对象.GetFiles()获得该文件夹下的文件,返回类型为FileInfo
    1. DirectoryInfo对象.Name获得文件夹名
    2. DirectoryInfo对象.FullName获得文件夹完整的路径名
    3. FileInfo对象.Name  和 FileInfo对象.FullName 同理

    程序使用以上方法,递归地输出当前运行程序所在的磁盘下的所有文件名和子目录名,并将结果保存在指定的txt文件中

    class Program
        {
            static void Main(string[] args)
            {
                //获取当前程序所在的文件路径
                String path = Directory.GetCurrentDirectory();
                String path2 = path.Substring(0, 3);     //取盘符
                StreamWriter sw=null;
                try{
                    //创建输出流,将得到文件名子目录名保存到txt中
                    sw = new StreamWriter(new FileStream("files.txt",FileMode.Append));
                    sw.WriteLine("根目录:" + path2);
                    getDirectory(sw,path2, 2);
                }
                catch(IOException e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    if (sw != null)
                    {
                        sw.Close();
                        Console.WriteLine("完成");
                    }
                }
     
                
            }
    
            /*
             * 获得指定路径下所有文件名
             * StreamWriter sw  文件写入流
             * string path      文件路径
             * int indent       输出时的缩进量
             */
            public static void getFileName(StreamWriter sw, string path,int indent)
            {
                DirectoryInfo root = new DirectoryInfo(path);
                foreach(FileInfo f in root.GetFiles())
                {
                    for (int i = 0; i < indent; i++)
                    {
                        sw.Write("  ");
                    }
                    sw.WriteLine(f.Name);
                }
            }
    
            //获得指定路径下所有子目录名
            public static void getDirectory(StreamWriter sw, string path,int indent)
            {
                getFileName(sw,path,indent);
                DirectoryInfo root = new DirectoryInfo(path);
                foreach(DirectoryInfo d in root.GetDirectories())
                {
                    for(int i = 0; i < indent; i++)
                    {
                        sw.Write("  ");
                    }
                    sw.WriteLine("文件夹:"+d.Name);
                    getDirectory(sw,d.FullName,indent+2);
                    sw.WriteLine();
                }
            }
        }

    结果:

  • 相关阅读:
    Java 8 Lambda 表达式
    OSGi 系列(十二)之 Http Service
    OSGi 系列(十三)之 Configuration Admin Service
    OSGi 系列(十四)之 Event Admin Service
    OSGi 系列(十六)之 JDBC Service
    OSGi 系列(十)之 Blueprint
    OSGi 系列(七)之服务的监听、跟踪、声明等
    OSGi 系列(六)之服务的使用
    OSGi 系列(三)之 bundle 事件监听
    OSGi 系列(三)之 bundle 详解
  • 原文地址:https://www.cnblogs.com/Drajun/p/7775266.html
Copyright © 2011-2022 走看看