A small FileFilter used to list files in a folder

Source: Internet
Author: User
A small list of file classes in a folder

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.