Search a types of file within the Directory with recursive in C#

No.of Views1384
Bookmarked0 times
Downloads 
Votes0
By  youngmurukan   On  15 Feb 2010 21:02:15
Tag : CSharp , Files and Folders
Search a types of file within the Directory with recursive in C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Hi Guys,

Following code hows to search types file within the Directory including sub directories.

{codecitation class="brush: c#; gutter: true;" width="700px"}

public class FileSearch
{
ArrayList _extensions;
bool _recursive;
public ArrayList SearchExtensions
{
get { return _extensions; }
}
public bool Recursive
{
get { return _recursive; }
set { _recursive = value; }
}
public FileSearch()
{
_extensions = ArrayList.Synchronized(new ArrayList());
_recursive = true;
}
public FileInfo[] Search(string path)
{
DirectoryInfo root = new DirectoryInfo(path);
ArrayList subFiles = new ArrayList();
foreach (FileInfo file in root.GetFiles())
{
if (_extensions.Contains(file.Extension))
{
subFiles.Add(file);
}
}
if (_recursive)
{
foreach (DirectoryInfo directory in root.GetDirectories())
{
subFiles.AddRange(Search(directory.FullName));
}
}
return (FileInfo[])subFiles.ToArray(typeof(FileInfo));
}
}

How to Use:

FileSearch fileser = new FileSearch();
fileser.Recursive = true;
fileser.SearchExtensions.Add(".rar");
FileInfo[] dirRar = fileser.Search("D:\\test");


{/codecitation}

That's all.Please share your comments through the info@codegain.com

Thank you

Murukan

 
Sign Up to vote for this article
 
About Author
 
youngmurukan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-12 May 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top