C#实现目标路径下文件递归的类

using System;
using System.IO;
using System.Collections;

namespace DSclub
{
    /** <summary>
    /// DirList 的摘要说明。
    /// </summary>
    public class DirList
    {
        private string strInitFilePath;
        private bool bFatchAll;

        // 构造函数
        public DirList()
        {
            bFatchAll = false;
            strInitFilePath = "C://";
        }
        public DirList(string strFilePath)
        {
            bFatchAll = false;
            strInitFilePath = strFilePath;
        }

        // 是否递归出所有的文件
        public bool RecursionFiles
        {
            get
            {
                return bFatchAll;
            }
            set
            {
                bFatchAll = value;
            }
        }

        // 取得文件的函数
        public ArrayList GetFiles()
        {
            return GetFiles(strInitFilePath, bFatchAll);
        }

        public static ArrayList GetFiles(string strPath, bool ResultsAll)
        {
            ArrayList al  = new ArrayList();
            // 判断路径是否存在
            if(!Directory.Exists(strPath))
            {
                throw(new ApplicationException("访问的路径" + strPath + "不存在,或者它不是个文件夹。"));
            }

            string[] temp =  Directory.GetFiles(strPath);
            foreach(string aFile in temp)
            {
                al.Add(aFile);
            }

            // 如果此目录下不存在文件,则把文件夹路径返回,并用///作标识
            if(temp.Length == 0)
            {
                al.Add("///" + strPath);
            }

            if(ResultsAll)
            {
                temp = Directory.GetDirectories(strPath);
                foreach(string aDir in temp)
                {
                    al.AddRange(GetFiles(aDir, ResultsAll));
                }
            }
           
            return al;
        }

    }
}
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值