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);
}
}
Joe WilsonPosted Dec 28, 2015, 10:39 AM
Thank you very much.
Hoai Ba DinhPosted Feb 22, 2012, 12:59 PM
thank you
nilesh warudePosted Dec 29, 2009, 4:17 AM
please
Maria praveeneditedPosted May 12, 2009, 8:54 AMEdited May 12, 2009, 8:59 AM
HI i am having Textbox,Button and Listbox, When i give any Text in that Textbox and click button, That text should be search in my Listbox. If that Text in my Listbox item, That Particular file(item) display in my listbox... plz help me... Very very urgent plz...................
kamal kalsiPosted Apr 23, 2009, 10:04 AM
to execute this code ,from where i get winforms assemblers code......................... can any one helps me...........
DanPosted Jan 13, 2009, 4:37 PM
Can anyone to post here the solution?
Atiqur RahmanPosted Jan 11, 2009, 12:11 AM
I've created a application using this cs file. But I get an error in the following method public Directory [] getDirectories(String strDrive) { Directory dir= new Directory(strDrive); Directory [] childDirs=dir.GetDirectories(); return childDirs; } What's the solution???
anurag kharePosted Dec 6, 2005, 1:37 AM
tell me the way n the book of c#