Tuesday, March 23, 2010

Recursive_Files_Search_In_CSharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Trans829115.BLL
{
///
/// RecursiveFilesSearch from FolderPath/DrivePath
///

///


public class RecursiveFilesSearch
{

static private List FilesFound = new List(); // DirectSearch(Global)

///
///
///

///
public RecursiveFilesSearch()
{

}

private string PaddAsDirectoryPath(string path)
{
if(string.IsNullOrEmpty(path)) return string.Empty;


if(path.Substring((path.Length-2))!="\\")
return path+"\\";

return string.Empty;

}


public enum EnumSourceFilesType
{
CS_Files,
VB_Files
}
///
/// Get All the files in the respective Folder/Drive
///

///
///
///
public List GetAllFileNames(string SourceDir, bool blnIsFileValidator)
{
if(!Directory.Exists(SourceDir))
{
throw new Exception(string.Format("{0} - is not exists - Please define valid Source Path", SourceDir ));
return null;
}

SourceDir = PaddAsDirectoryPath(SourceDir);

List files = new List();

DirectoryInfo di = new DirectoryInfo(SourceDir);

DirectoryInfo[] dirs = di.GetDirectories();

foreach (DirectoryInfo diNext in dirs)
{
files.AddRange(DirSearch(diNext.FullName, "*.*", blnIsFileValidator));
}

return files;

}

///
/// Specific search on SearchType
///

///
///
///

public List GetSelectedFileNames(string SourceDir, EnumSourceFilesType enumSourceType, bool blnIsFileValidator)
{
if(!Directory.Exists(SourceDir))
{
throw new Exception(string.Format("{0} - is not exists - Please define valid Source Path", SourceDir ));
return null;
}

string searchPattern = "*.*"; // Default

switch(enumSourceType)
{
case EnumSourceFilesType.CS_Files:
searchPattern = "*.cs";
break;
case EnumSourceFilesType.VB_Files:
searchPattern = "*.vb";
break;
}

SourceDir = PaddAsDirectoryPath(SourceDir);

List files = new List();

DirectoryInfo di = new DirectoryInfo(SourceDir);

DirectoryInfo[] dirs = di.GetDirectories();

foreach (DirectoryInfo diNext in dirs)
{
files.AddRange(DirSearch(diNext.FullName, searchPattern, blnIsFileValidator));
}

return files;

}



private List DirSearch(string sDir, string SearchPattern)
{

try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, SearchPattern))
{
FilesFound.Add(f);
}
DirSearch(d,SearchPattern);
}

return FilesFound;
}
catch (System.Exception excpt)
{
throw new Exception(excpt.Message);
}
}

///
/// Option to apply FileValidator method
///

///
///
///
///
private List DirSearch(string sDir, string SearchPattern, bool blnIsFileValidator)
{

try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, SearchPattern))
{
if(blnIsFileValidator)
{
if(ValidateFile(f))
FilesFound.Add(f);
}
else
FilesFound.Add(f);
}
DirSearch(d, SearchPattern, blnIsFileValidator);
}

return FilesFound;
}
catch (System.Exception excpt)
{
return FilesFound; // Intermediate results to pass on
throw new Exception(excpt.Message);
}
}
///
/// check Is File is Valid for Copying
///

///
///
private bool ValidateFile(string file)
{
file = Path.GetFileName(file);
if(string.IsNullOrEmpty(file)) return false;

if(file.Equals("Program.cs")) return false;

if(file.Equals("Program.vb")) return false;

if(file.Contains("Designer.cs")) return false;

if(file.Contains("Designer.vb")) return false;

if(file.Equals("AssemblyInfo.cs")) return false;

if(file.Equals("AssemblyInfo.vb")) return false;

return true;
}
}
}
 
Locations of visitors to this page