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

此博客展示了一段C#代码,定义了DirList类用于获取目录下的文件。包含构造函数、判断是否递归获取文件的属性及获取文件的方法。代码中未进行系统文件检查和用户不可访问文件夹判断,采用递归思想实现功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gifusing System;
None.gif
using System.IO;
None.gif
using System.Collections;
None.gif
None.gif
namespace DSclub
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DirList 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DirList
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string strInitFilePath;
InBlock.gif        
private bool bFatchAll;
InBlock.gif
InBlock.gif        
// 构造函数
InBlock.gif
        public DirList()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            bFatchAll 
= false;
InBlock.gif            strInitFilePath 
= "C:\\";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public DirList(string strFilePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            bFatchAll 
= false;
InBlock.gif            strInitFilePath 
= strFilePath;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 是否递归出所有的文件
InBlock.gif
        public bool RecursionFiles
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return bFatchAll;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bFatchAll 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 取得文件的函数
InBlock.gif
        public ArrayList GetFiles()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetFiles(strInitFilePath, bFatchAll);
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
InBlock.gif            
string[] temp =  Directory.GetFiles(strPath);
InBlock.gif            
foreach(string aFile in temp)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                al.Add(aFile);
ExpandedSubBlockEnd.gif            }

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

InBlock.gif
InBlock.gif            
if(ResultsAll)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                temp 
= Directory.GetDirectories(strPath);
InBlock.gif                
foreach(string aDir in temp)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    al.AddRange(GetFiles(aDir, ResultsAll));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
return al;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

其中关于应该有系统文件的检查,还有用户不可访问系统文件夹的判断,但是这个项目中用不上,又不想用Try块儿影响效率。

还是递归的思想!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值