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

来源:互联网 发布:淘宝客服认证二维码 编辑:程序博客网 时间:2024/04/28 03:50
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;
        }


    }

}