Find Files in C#

I have developed a small application in c#, which helps in searching u'r hard disk for files of the desired extention. I have used System.IO namespace from the .Net framework. So now gone are the days of importing the API's functions in VB or using the fileSystemObject(and adding one more activex component with u'r builds!!!!!) for searching for doing file operations.The System.IO namespace gives us very effective classes for file specific operations.

Source Code

//Author : Bejoy Nair
//Company : Patni Computers,Pune.
//Dated : 04-27-2001.
//Comments: This is an application written in C#
for searching files on u'r hard drives.
// The namespaces used are System.IO,System.Drawing
and System.Winforms.
// To compile the code give the below compiler options
// csc /r:System.dll /r:System.WinForms.dll /
r:System.Drawing.dll /r:Microsoft.Win32.Interop.dll FindFile.cs
//Tools : TextPad and .Net S.
using System;
using System.WinForms;
using System.Drawing;
using System.IO;
public class frmFind : Form
{
private int nItems=0;
static ListBox listBox1=new ListBox();
static ComboBox cmbDrives= new ComboBox();
static TextBox txtSearchType= new TextBox();
static Label lblFileType = new Label();
static Label lblFolder = new Label();
static Label lblHeader = new Label();
BorderStyle x= new BorderStyle();
Button button1 = new Button();
Button button2 = new Button();
public frmFind()
{
this.Text="File Search Application using C#";
this.EventHandler();
}
// Create the form and add the required buttons.
public void InitializeForm()
{
this.BorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.button1.Text="Get Data";
this.button2.Text="Exit";
this.BackColor=Color.White;
this.button1.Location=new Point(70,245);
this.button2.Location=new Point(this.button1.Left + this.button1.Left + 30,this.button1.Top);
this.button1.BackColor=Color.DeepSkyBlue;
this.button2.BackColor=Color.DeepSkyBlue;
this.button1.TabIndex=3; 
// set the location of the Label.
// lblFileType.BorderStyle = BorderStyle.None;
lblFileType.Text ="File Type:";
lblFileType.Location=new Point(10,30);
lblFolder.Text ="Look In:";
lblFolder.Location=new Point(10,60);
lblHeader.Text="Select the drive and Enter the file type to be searched!";
lblHeader.AutoSize=true;
lblHeader.Location=new Point(20,5);
// Set the location of the ListBox.
listBox1.Location=new Point(70,90);
listBox1.BackColor=Color.Aqua;
listBox1.Width=200;
listBox1.Height=150;
listBox1.HorizontalScrollbar=true;
// Set the location of the comboBox.
cmbDrives.Location=new Point(70,60);
cmbDrives.BackColor=Color.Aqua;
cmbDrives.Width=200;
// Set the location of the search text field.
txtSearchType.Location=new Point(70,30);
txtSearchType.BackColor=Color.Aqua;
txtSearchType.Width=200;
txtSearchType.TabIndex=0;
//Add the button to the form.
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.Controls.Add(listBox1);
this.Controls.Add(cmbDrives);
this.Controls.Add(txtSearchType);
this.Controls.Add(lblFileType);
this.Controls.Add(lblFolder);
this.Controls.Add(lblHeader);
this.StartPosition = FormStartPosition.CenterScreen;
}
public void AddComboBoxItem(String [] strDrives)
{
// listBox1.InsertItem(nItems,oItem);
for(int i=0;i<strDrives.Length;i++)
{
cmbDrives.Items.Add(strDrives[i]);
++nItems;
}
}
public void AddListBoxItem()
{
listBox1.InsertItem(0,cmbDrives.Text);
}
public static int Main(string[] args)
{
frmFind frmFindApp= new frmFind();
// Create the form.
frmFindApp.InitializeForm();
// Get the logical drives for the machine.
string [] strDrives=frmFindApp.getDrives();
frmFindApp.AddComboBoxItem(strDrives);
// Display the form as a modal dialog box.
frmFindApp.ShowDialog();
return 0;
}
// Event Handler for buttons for getting the folders.
protected void button1_Click(object sender,System.EventArgs e)
{
string strMessage="No Matches";
listBox1.Items.Clear();
if((txtSearchType.Text=="")&&(cmbDrives.Text==""))
{
lblHeader.Text="Select the drive and Enter the file type to be searched!";
}
else if(txtSearchType.Text=="")
{
lblHeader.Text="Enter the file type to be searched!";
}
else if(cmbDrives.Text=="")
{
lblHeader.Text="Select the drive to search the files!";
}
else
{
try
{
int i=0;
int iVal;
Directory []ChildDirs= this.getDirectories(cmbDrives.Text);
//If the Search file type has no extention then add one.
iVal=txtSearchType.Text.IndexOf(".");
if(iVal==-1)
{
txtSearchType.Text+=".*";
}
//Get the Child Directories.
foreach(Directory ChildDir in ChildDirs)
{
//recurse through the child directories.
while(ChildDir.GetDirectories().Length>0)
{
Directory []GrandChilds=ChildDir.GetDirectories();
foreach(Directory GrandChild in GrandChilds)
{
File [] Files = GrandChild.GetFiles(txtSearchType.Text);
if(Files.Length==0)
{
// Do nothing.
}
else
{
foreach(File DirFile in Files)
{
listBox1.InsertItem(i,DirFile.Name);
i++;
}
}
ChildDir=GrandChild;
}
}
}
}
catch(IOException E)
{
strMessage=E.Message;
listBox1.InsertItem(0,strMessage);
strMessage="";
}
if (listBox1.Items.Count==0)
{
listBox1.InsertItem(0,strMessage);
}
} 
} 
public String [] getDrives()
{
return Directory.GetLogicalDrives();
}
public Directory [] getDirectories(String strDrive)
{
Directory dir= new Directory(strDrive);
Directory [] childDirs=dir.GetDirectories();
return childDirs;
}
protected void button2_Click(object sender,System.EventArgs e)
{
//Close the list box.
this.Close();
} 
protected void cmbDrives_SelectedIndexChanged(object sender,System.EventArgs e)
{
}
//register the event handler.
public void EventHandler()
{
this.button1.Click+=new System.EventHandler(this.button1_Click);
this.button2.Click+=new System.EventHandler(this.button2_Click);
cmbDrives.SelectedIndexChanged+=new System.EventHandler(cmbDrives_SelectedIndexChanged);
}
}


Similar Articles