using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SearchFile
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class MainApp : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ColumnHeader chName;
		private System.Windows.Forms.ColumnHeader chDirectory;
		private System.Windows.Forms.ColumnHeader chSize;
		private System.Windows.Forms.ColumnHeader chLastWriteTime;
		private System.Windows.Forms.TextBox txtSearchFileName;
		private System.Windows.Forms.ComboBox cmbDrives;
		private System.Windows.Forms.Button btnStart;
		private System.Windows.Forms.Button btnStop;
		private System.Windows.Forms.ListView lvResult;
		private System.Windows.Forms.ImageList imageList;
		private System.ComponentModel.IContainer components;

		public MainApp()
		{
			InitializeComponent();
			// 取得所有逻辑驱动器
			string [] strDrives=getDrives();
			AddComboBoxItem(strDrives);
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainApp));
			this.panel1 = new System.Windows.Forms.Panel();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.label1 = new System.Windows.Forms.Label();
			this.txtSearchFileName = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.cmbDrives = new System.Windows.Forms.ComboBox();
			this.lvResult = new System.Windows.Forms.ListView();
			this.chName = new System.Windows.Forms.ColumnHeader();
			this.chDirectory = new System.Windows.Forms.ColumnHeader();
			this.chSize = new System.Windows.Forms.ColumnHeader();
			this.chLastWriteTime = new System.Windows.Forms.ColumnHeader();
			this.btnStart = new System.Windows.Forms.Button();
			this.btnStop = new System.Windows.Forms.Button();
			this.imageList = new System.Windows.Forms.ImageList(this.components);
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.btnStop,
																				 this.btnStart,
																				 this.cmbDrives,
																				 this.label2,
																				 this.txtSearchFileName,
																				 this.label1});
			this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(192, 273);
			this.panel1.TabIndex = 0;
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(192, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(8, 273);
			this.splitter1.TabIndex = 1;
			this.splitter1.TabStop = false;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(160, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "要搜索的文件或文件夹名为:";
			// 
			// txtSearchFileName
			// 
			this.txtSearchFileName.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtSearchFileName.Location = new System.Drawing.Point(16, 48);
			this.txtSearchFileName.Name = "txtSearchFileName";
			this.txtSearchFileName.Size = new System.Drawing.Size(160, 22);
			this.txtSearchFileName.TabIndex = 1;
			this.txtSearchFileName.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 80);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(160, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "搜索范围:";
			// 
			// cmbDrives
			// 
			this.cmbDrives.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cmbDrives.Location = new System.Drawing.Point(16, 104);
			this.cmbDrives.Name = "cmbDrives";
			this.cmbDrives.Size = new System.Drawing.Size(160, 22);
			this.cmbDrives.TabIndex = 3;
			// 
			// lvResult
			// 
			this.lvResult.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					   this.chName,
																					   this.chDirectory,
																					   this.chSize,
																					   this.chLastWriteTime});
			this.lvResult.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lvResult.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lvResult.Location = new System.Drawing.Point(200, 0);
			this.lvResult.Name = "lvResult";
			this.lvResult.Size = new System.Drawing.Size(280, 273);
			this.lvResult.SmallImageList = this.imageList;
			this.lvResult.TabIndex = 2;
			this.lvResult.View = System.Windows.Forms.View.Details;
			// 
			// chName
			// 
			this.chName.Text = "名称";
			// 
			// chDirectory
			// 
			this.chDirectory.Text = "所在文件夹";
			this.chDirectory.Width = 73;
			// 
			// chSize
			// 
			this.chSize.Text = "大小";
			// 
			// chLastWriteTime
			// 
			this.chLastWriteTime.Text = "修改时间";
			// 
			// btnStart
			// 
			this.btnStart.Location = new System.Drawing.Point(16, 152);
			this.btnStart.Name = "btnStart";
			this.btnStart.TabIndex = 4;
			this.btnStart.Text = "立即搜索";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// btnStop
			// 
			this.btnStop.Location = new System.Drawing.Point(104, 152);
			this.btnStop.Name = "btnStop";
			this.btnStop.TabIndex = 5;
			this.btnStop.Text = "停止搜索";
			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
			// 
			// imageList
			// 
			this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
			this.imageList.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
			this.imageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// MainApp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(480, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.lvResult,
																		  this.splitter1,
																		  this.panel1});
			this.Name = "MainApp";
			this.Text = "文件搜索器";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainApp());
			      		
		}

		public void AddComboBoxItem(String [] strDrives)
		{
			for(int i=0;i<strDrives.Length;i++) 			
				cmbDrives.Items.Add(strDrives[i]);
		}   

		public void AddListViewItem(string filename,string directory,string size,string lastwritetime)
		{
			ListViewItem lvi;
			lvi=new ListViewItem(new String[]{filename,directory,size,lastwritetime},4);
			lvi.ImageIndex=0;
			lvResult.Items.Add(lvi);
		}
   
		public String [] getDrives()
		{
			return Directory.GetLogicalDrives();
		}
 
  
		public DirectoryInfo [] getDirectories(String strDrive) 
		{
			DirectoryInfo dir= new DirectoryInfo(strDrive);
			DirectoryInfo [] childDirs=dir.GetDirectories();
			return childDirs;
		}
 
	
		private void btnStart_Click(object sender, System.EventArgs e)
		{
			lvResult.Items.Clear();
        
			if((txtSearchFileName.Text=="")&&(cmbDrives.Text==""))
			{
				MessageBox.Show("选择一个驱动器，然后输入文件名!");
			}	
    		else if(txtSearchFileName.Text=="")
			{
				MessageBox.Show("输入文件名!");
			}
			else if(cmbDrives.Text=="")
			{
				MessageBox.Show("选择一个驱动器!");
			}
			else 
			{        
				try
				{
					int i=0;  
					int iVal;
					DirectoryInfo []ChildDirs= this.getDirectories(cmbDrives.Text);
		
					//如果搜索的文件名没有扩展名，则后面加上".*"
					iVal=txtSearchFileName.Text.IndexOf(".");
					if(iVal==-1)
					{
						txtSearchFileName.Text+=".*";
					}
		
					//取得子文件夹
					DirectoryInfo ChildDir;
					foreach(DirectoryInfo di in ChildDirs) 
					{
						ChildDir=di;

						//递归搜索子文件夹
						while(ChildDir.GetDirectories().Length>0)
						{
							DirectoryInfo []GrandChilds=ChildDir.GetDirectories(); 

							foreach(DirectoryInfo GrandChild in GrandChilds) 
							{
								FileInfo [] Files = GrandChild.GetFiles(txtSearchFileName.Text);
								if(Files.Length==0)
								{
									// 什么都不做
								}
								else
								{  
									foreach(FileInfo DirFile in Files) 
									{
										AddListViewItem(DirFile.Name,DirFile.Directory.FullName,DirFile.Length.ToString(),DirFile.LastWriteTime.ToShortDateString());
										i++;
									}
								} 
								ChildDir=GrandChild;     	
							}
						}	       
					}
				}
				catch(IOException ioex)
				{
					MessageBox.Show(ioex.Message);
				}

				if (lvResult.Items.Count==0)
				{
					MessageBox.Show("搜索完毕,没有结果可以显示。");
      
				}
			}      
           
		
		}

		private void btnStop_Click(object sender, System.EventArgs e)
		{
			this.Close();
		} 
   
   	
	}
}
