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

时间:2022-12-20 21:36:13

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

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);
  }
 }
}

相关文章