文件夹遍历[合成模式]
这样做了感觉多此一举,不过当初学习合成模式的时候,想到的。
效率低,只有一个目的,帮助理解合成模式,也不知道能不能起到这样的作用。
不要笑话我哦:)
![](/images/cnblogs_com/luoboqingcai/ClassDiagram2.png)
效率低,只有一个目的,帮助理解合成模式,也不知道能不能起到这样的作用。
不要笑话我哦:)
![](/images/cnblogs_com/luoboqingcai/ClassDiagram2.png)
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace PatternsStudy6_1
{
class Program
{
private static int intCount = 0;
/// <summary>
/// 记录文件个数
/// </summary>
public static int IntCount
{
get
{
return intCount;
}
}
static void Main(string[] args)
{
string strDirName = @"E:\Drivers";
Search_Folder root = GetRoot(strDirName);
root.Display(1);
Console.WriteLine(IntCount.ToString() + " -files");
Console.Read();
}
/// <summary>
/// 取得Search_Folder实例
/// </summary>
/// <param name="strDirName">文件夹名</param>
/// <returns></returns>
static Search_Folder GetRoot(string strDirName)
{
Search_Folder root = new Search_Folder(strDirName);
//取得strDirName下所有文件名
string[] strFiles = Directory.GetFiles(strDirName);
foreach (string str in strFiles)
{
root.Add(new Search_File(str));
intCount++;
}
//取得strDirName下所有文件夹名
string[] strFolders = Directory.GetDirectories(strDirName);
foreach (string str in strFolders)
{
//自身调用,递归
root.Add(GetRoot(str));
}
return root;
}
}
/// <summary>
/// 搜索的抽象基类
/// </summary>
abstract class Search
{
/// <summary>
/// 名称
/// </summary>
protected string m_strName;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="strName">名称</param>
public Search(string strName)
{
m_strName = strName;
}
/// <summary>
/// 用于屏幕显示的抽象函数
/// </summary>
/// <param name="intDept">深度</param>
public abstract void Display(int intDept);
}
/// <summary>
/// 文件
/// </summary>
class Search_File : Search
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="strName">文件名</param>
public Search_File(string strName)
: base(strName)
{ }
/// <summary>
/// 屏幕显示
/// </summary>
/// <param name="intDept">深度</param>
/// <remarks>显示形式: --文件名</remarks>
public override void Display(int intDept)
{
Console.WriteLine(new String('-', intDept) + base.m_strName);
}
}
/// <summary>
/// 文件夹
/// </summary>
class Search_Folder : Search
{
List<Search> list = new List<Search>();
public override void Display(int intDept)
{
Console.WriteLine(new String('-', intDept - 1) + "+" + base.m_strName);
foreach (Search search in list)
{
search.Display(intDept + 2);
}
}
public Search_Folder(string strName)
: base(strName)
{ }
/// <summary>
/// 添加操作
/// </summary>
/// <param name="search">Search对象</param>
public void Add(Search search)
{
list.Add(search);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace PatternsStudy6_1
{
class Program
{
private static int intCount = 0;
/// <summary>
/// 记录文件个数
/// </summary>
public static int IntCount
{
get
{
return intCount;
}
}
static void Main(string[] args)
{
string strDirName = @"E:\Drivers";
Search_Folder root = GetRoot(strDirName);
root.Display(1);
Console.WriteLine(IntCount.ToString() + " -files");
Console.Read();
}
/// <summary>
/// 取得Search_Folder实例
/// </summary>
/// <param name="strDirName">文件夹名</param>
/// <returns></returns>
static Search_Folder GetRoot(string strDirName)
{
Search_Folder root = new Search_Folder(strDirName);
//取得strDirName下所有文件名
string[] strFiles = Directory.GetFiles(strDirName);
foreach (string str in strFiles)
{
root.Add(new Search_File(str));
intCount++;
}
//取得strDirName下所有文件夹名
string[] strFolders = Directory.GetDirectories(strDirName);
foreach (string str in strFolders)
{
//自身调用,递归
root.Add(GetRoot(str));
}
return root;
}
}
/// <summary>
/// 搜索的抽象基类
/// </summary>
abstract class Search
{
/// <summary>
/// 名称
/// </summary>
protected string m_strName;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="strName">名称</param>
public Search(string strName)
{
m_strName = strName;
}
/// <summary>
/// 用于屏幕显示的抽象函数
/// </summary>
/// <param name="intDept">深度</param>
public abstract void Display(int intDept);
}
/// <summary>
/// 文件
/// </summary>
class Search_File : Search
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="strName">文件名</param>
public Search_File(string strName)
: base(strName)
{ }
/// <summary>
/// 屏幕显示
/// </summary>
/// <param name="intDept">深度</param>
/// <remarks>显示形式: --文件名</remarks>
public override void Display(int intDept)
{
Console.WriteLine(new String('-', intDept) + base.m_strName);
}
}
/// <summary>
/// 文件夹
/// </summary>
class Search_Folder : Search
{
List<Search> list = new List<Search>();
public override void Display(int intDept)
{
Console.WriteLine(new String('-', intDept - 1) + "+" + base.m_strName);
foreach (Search search in list)
{
search.Display(intDept + 2);
}
}
public Search_Folder(string strName)
: base(strName)
{ }
/// <summary>
/// 添加操作
/// </summary>
/// <param name="search">Search对象</param>
public void Add(Search search)
{
list.Add(search);
}
}
}