📄 mdisearch.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
namespace MDITextEditor
{
/// <summary>
/// MDISearch 的摘要说明。
/// </summary>
public class MDISearch : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtSearchFileName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cmbDrives;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.ListView lvResult;
private System.Windows.Forms.ColumnHeader chName;
private System.Windows.Forms.ColumnHeader chDirectory;
private System.Windows.Forms.ColumnHeader chSize;
private System.Windows.Forms.ColumnHeader chLastWriteTime;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public MDISearch()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
string [] strDrives=getDrives();
this.AddComboBox(strDrives);
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.splitter1 = new System.Windows.Forms.Splitter();
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.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.cmbDrives);
this.panel1.Controls.Add(this.txtSearchFileName);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.label2);
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(224, 326);
this.panel1.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(200, 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(8, 48);
this.txtSearchFileName.Name = "txtSearchFileName";
this.txtSearchFileName.Size = new System.Drawing.Size(184, 22);
this.txtSearchFileName.TabIndex = 1;
this.txtSearchFileName.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(200, 16);
this.label2.TabIndex = 0;
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(8, 112);
this.cmbDrives.Name = "cmbDrives";
this.cmbDrives.Size = new System.Drawing.Size(184, 22);
this.cmbDrives.TabIndex = 2;
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 192);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "立即搜索";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(128, 192);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "停止搜索";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(224, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 326);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
//
// 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(227, 0);
this.lvResult.Name = "lvResult";
this.lvResult.Size = new System.Drawing.Size(341, 326);
this.lvResult.TabIndex = 2;
this.lvResult.View = System.Windows.Forms.View.Details;
//
// chName
//
this.chName.Text = "名称";
this.chName.Width = 69;
//
// chDirectory
//
this.chDirectory.Text = "所在文件夹";
this.chDirectory.Width = 81;
//
// chSize
//
this.chSize.Text = "大小";
this.chSize.Width = 68;
//
// chLastWriteTime
//
this.chLastWriteTime.Text = "修改时间";
this.chLastWriteTime.Width = 83;
//
// MDISearch
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 326);
this.Controls.Add(this.lvResult);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel1);
this.Name = "MDISearch";
this.Text = "MDISearch";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
public void AddComboBox(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 button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button1_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("搜索完毕,没有结果可以显示。");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -