C#遍历目录下的所有文件夹

//这个是网上转的 上一个也是仿照这个改的 同样是win32控制台程序  学习!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace TestFileInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            string pathNames=null;            
            do
            {
                Console.Write("请输入目录路径名(输入x退出程序):");
                pathNames = Console.ReadLine();
                if ((pathNames == "X") || (pathNames == "x"))
                    break;
                DirectoryInfo dirPath = new DirectoryInfo(pathNames);
                CDirectoryInfo dirObject = new CDirectoryInfo();
                Console.Write(dirObject.GetDirectoryInfos(dirPath));                
                Console.WriteLine("______________________________________________________/n");
                //dirObject.pathNameProp = null;
                //Console.Write(dirObject.GetDirectoryInfos(pathNames));
                //Console.WriteLine("_____________________________________________________");
            }
            while (true);//((pathName == "X") || (pathName == "x"));
            Console.Write("/n程序已终止,按任意键退出程序!");
            Console.ReadKey();            
        }
    }

    class CDirectoryInfo
    {
        string pathName=null;
        public string pathNameProp
        {
            set
            {
                pathName = value;
            }
            get
            {
                return pathName;
            }
        }

        /// <summary>
        /// 遍历核心代码
        /// </summary>
        /// <param name="fatherDir"></param>
        /// <returns></returns>
        public string GetDirectoryInfos(DirectoryInfo fatherDir)
        {
            try
            {
                DirectoryInfo[] childDir = fatherDir.GetDirectories();
                if (childDir.Length == 0)                               //如果已无下层目录,则将当前目录的完整路径加入到pathName变量中
                    return pathName += fatherDir.FullName + "/n";
                else
                {
                    for (int i = 0; i < childDir.Length; i++)           //如果有下层目录,则递归每一个下层目录,且在第一次循环时                                                                    
                    {                                                   //将当前目录的完整路径加入到pathName变量中加入
                        if (i == 0)
                            pathName += fatherDir.FullName + "/n";
                        GetDirectoryInfos(childDir[i]);
                    }
                    return pathName;
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                return "你输入的路径无效,它可能不存在或者该路径拒绝访问。/n相关提示:/n"
                        + ex.Message + "/n";
            }
            catch (UnauthorizedAccessException ex)
            {
                return "你输入的路径无效,该路径可能拒绝访问。/n相关提示:/n"
                        + ex.Message + "/n";
            }
        }

        /// <summary>
        /// 遍历文件夹的重载函数
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public string GetDirectoryInfos(string pathName)
        {
            try
            {
                DirectoryInfo fatherDir = new DirectoryInfo(pathName);
                DirectoryInfo[] childDir = fatherDir.GetDirectories();
                if (childDir.Length == 0)
                    return this.pathName += fatherDir.FullName + "/n";
                else
                {
                    for (int i = 0; i < childDir.Length; i++)
                    {
                        if (i == 0)
                            this.pathName += fatherDir.FullName + "/n";
                        GetDirectoryInfos(childDir[i].FullName);
                    }
                    return this.pathName;
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                return "你输入的路径无效,它可能不存在或者该路径拒绝访问。/n相关提示:/n"
                        + ex.Message + "/n";
            }
            catch (UnauthorizedAccessException ex)
            {
                return "你输入的路径无效,该路径可能拒绝访问。/n相关提示:/n"
                        + ex.Message + "/n";
            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值