//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.Windows.Forms;
   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.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.Items.Insert(nItems,oItem);
      	for(int i=0;i<strDrives.Length;i++) {
      	   cmbDrives.Items.Add(strDrives[i]);
           ++nItems;
        }
     }   

   public void AddListBoxItem(){
        listBox1.Items.Insert(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;
		DirectoryInfo []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.
		DirectoryInfo ChildDir;
		foreach(DirectoryInfo di in ChildDirs) {
			ChildDir=di;

		//recurse through the child directories.
		  while(ChildDir.GetDirectories().Length>0){

			 DirectoryInfo []GrandChilds=ChildDir.GetDirectories(); 

			 foreach(DirectoryInfo GrandChild in GrandChilds) {
				FileInfo [] Files = GrandChild.GetFiles(txtSearchType.Text);
				if(Files.Length==0){
				// Do nothing.   

				   }
				else{  
				foreach(FileInfo DirFile in Files) {
				   listBox1.Items.Insert(i,DirFile.FullName);
				   i++;

				   }
			} 

			 ChildDir=GrandChild;     	
		  }



		   }	       

		}
	     }catch(IOException E){
	       strMessage=E.Message;
	       listBox1.Items.Insert(0,strMessage);
	       strMessage="";
	     }

      if (listBox1.Items.Count==0){
      	   listBox1.Items.Insert(0,strMessage);
      	   
	    }
      }      
           
              
   }   
        
 
  public String [] getDrives(){
       return Directory.GetLogicalDrives();
       }
 
  
  public DirectoryInfo [] getDirectories(String strDrive) {
       DirectoryInfo dir= new DirectoryInfo(strDrive);
       DirectoryInfo [] 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) {
      	   
        }

   	private void InitializeComponent()
	   {
		// 
		// frmFind
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
		this.ClientSize = new System.Drawing.Size(292, 273);
		this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		this.Name = "frmFind";

	}

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








































