基本信息
源码名称:C# 用递归的方式查找指定文件夹下的所有子目录.rar
源码大小:3.80KB
文件格式:.rar
开发语言:C#
更新时间:2019-09-11
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.IO;
using System.Windows.Forms;
using System.Collections;
namespace RecursiveFileExplorer
{
public class FileExplorer
{
public ArrayList FileList = new ArrayList();
public ArrayList Extensions = null;
private string Path = "";
private bool Recursive = true;
/// <summary>
/// Constructor called if filepath argument given.
/// </summary>
/// <param name="filepath">Initial path to search from.</param>
public FileExplorer( string path )
{
this.Path = path;
this.Recursive = false;
this.FileList = this.getFiles();
}
/// <summary>
/// Constructor called if filepath and extensions arguments given.
/// </summary>
/// <param name="filepath">Initial path to search from.</param>
/// <param name="extensions">Arraylist of file extensions to filter results by.</param>
public FileExplorer( string path, ArrayList extensions )
{
this.Path = path;
this.Extensions = extensions;
this.Recursive = false;
this.FileList = this.getFiles();
}
/// <summary>
/// Constructor called if filepath and recursive arguments given.
/// </summary>
/// <param name="filepath">Initial path to search from.</param>
/// <param name="recursive">Specifies whether to use recursion to search sub-folders.</param>
public FileExplorer( string path, bool recursive )
{
this.Path = path;
this.Recursive = recursive;
this.FileList = this.getFiles();
}
/// <summary>
/// Constructor called if filepath, extensions and recursive arguments given.
/// </summary>
/// <param name="filepath">Initial path to search from.</param>
/// <param name="extensions">Arraylist of file extensions to filter results by.</param>
/// <param name="recursive">Specifies whether to use recursion to search sub-folders.</param>
public FileExplorer( string path, ArrayList extensions, bool recursive )
{
this.Path = path;
this.Extensions = extensions;
this.Recursive = recursive;
this.FileList = this.getFiles();
}
/// <summary>
/// Searches through directory and sub-directories for files.
/// </summary>
/// <returns>ArrayList of files found.</returns>
public ArrayList getFiles()
{
DirectoryInfo dir = new DirectoryInfo( this.Path );
FileInfo[] files;
DirectoryInfo[] dirs;
ArrayList List = new ArrayList();
//if the source dir doesn't exist, throw
if (! dir.Exists)
{
throw new Exception("Source directory doesn't exist: " this.Path );
}
//get all files in the current dir
files = dir.GetFiles();
//loop through each file
foreach(FileInfo file in files)
{
if( this.Extensions == null )
{
List.Add( new FileData( file.FullName, file.Name, file.Extension, file.Length ) );
}
else
{
if( this.checkFile( file.Extension ) )
{
List.Add( new FileData( file.FullName, file.Name, file.Extension, file.Length ) );
}
}
}
//cleanup
files = null;
//if not recursive, all work is done
if (! this.Recursive)
{
return List;
}
//otherwise, get dirs
dirs = dir.GetDirectories();
//loop through each sub directory in the current dir
foreach(DirectoryInfo subdir in dirs)
{
this.Path = subdir.FullName;
ArrayList temp = new ArrayList();
temp = getFiles();
for(int i=0; i < temp.Count; i )
{
FileData TempData = (FileData)temp[i];
List.Add( new FileData( TempData.FullName, TempData.Name, TempData.Extension, TempData.Length ) );
}
}
//cleanup
dirs = null;
dir = null;
return List;
}
/// <summary>
/// Matches current file extension against given file extension.
/// </summary>
/// <param name="strExtension">Extension to match against.</param>
/// <returns>Whether current file extension matches given file extension.</returns>
private bool checkFile( string strExtension )
{
bool throwaway = true;
for(int j=0; j<this.Extensions.Count; j )
{
if( this.Extensions[j].ToString() == strExtension )
{
throwaway = false;
}
}
if( throwaway )
{
return false;
}
else
{
return true;
}
}
}
}