一个小巧的列出文件夹下的文件类

using System;

namespace CodeBuilder.Builder
{
 class MainClass
 {
  public static void Main(string[] args)
 {
//   for(int f = 0; f < 4; f++)
//   {
   int f = 3;
   System.Console.WriteLine("_______________{Begin}_______________");
   string folder = System.Console.ReadLine();
    FileFilter filter = new FilterFactory().CreateInstance(f);
    ListFolder listfolder = new ListFolder(folder,filter);
    System.Collections.ArrayList al = listfolder.Files;
    if (al != null)
    {
     for(int i = 0; i < al.Count; i++)
     {
      System.Console.WriteLine("_____{0}_____",i);
      System.Console.WriteLine(al[i]);
     }
    }
    
    System.Console.WriteLine("_______________{End}_______________");
    System.Console.ReadLine();

//   }
  }
 }
 
 public class FilterFactory
 {
  public FileFilter CreateInstance(int i)
  {
   switch (i)
   {
    case 0:
     return  new WFilter();
    case 1:
     return  new XFilter();
    case 2:
     return  new YFilter();
    case 3:
     return  new ZFilter();
    default:
     return  new FileFilter();
   }
  }

  public FileFilter CreateInstance(string  str)
  {
   switch (str)
   {
    case ".cs":
     return  new WFilter();
    case ".aspx":
     return  new XFilter();
    case ".aspx.cs":
     return  new YFilter();
    case ".xml":
     return  new ZFilter();
    default:
     return  new FileFilter();
   }
  }
 }
 public class ZFilter:FileFilter
 {
  public  override bool Filter(string filename)
  {
   return filename.ToLower().EndsWith(".xml");
  }
 }

 public class YFilter:FileFilter
 {
  public  override bool Filter(string filename)
  {
   return filename.ToLower().EndsWith(".aspx.cs");
  }
 }
 
 public class XFilter:FileFilter
 {
  public  override bool Filter(string filename)
  {
   return filename.ToLower().EndsWith(".aspx");
  }
 }
 
 public class WFilter:FileFilter
 {
  public  override bool Filter(string filename)
  {
   return filename.ToLower().EndsWith(".cs");
  }
 }
 public class FileFilter
 {
  public virtual bool Filter(string filename)
  {
   return true;
  }
 }
 
 public sealed class ListFolder
 {
  System.Collections.ArrayList _Files = new System.Collections.ArrayList();
  FileFilter filefilter;
  public ListFolder(string path, FileFilter filter)
  {
   this.filefilter = filter;
   Folder(path);
  }
  
  public System.Collections.ArrayList Files
  {
   get {return _Files;}
  }
  
  void Folder(string path)
  {
   if (System.IO.File.Exists(path))
   {
    docase(path);
   }
   else if (System.IO.Directory.Exists(path))
   {
    string[] files = new string[0];
    files = System.IO.Directory.GetFiles(path);
    for(int i = 0 ; i < files.Length; i++)
    {
     docase(files[i]);
    }
    
    string[] folders = System.IO.Directory.GetDirectories(path);
    for(int i = 0; i < folders.Length; i++)
    {
     Folder(folders[i]);
    }
   }
  }
  
  void docase(string path)
  {
   if (filefilter.Filter(path)) _Files.Add(path);
  }
 }
}

来源:http://www.cnblogs.com/bigmouthz/articles/322580.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值