using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.Threading;
namespace Search
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.TextBox tbInfo;
private System.Windows.Forms.StatusBar sbStatus;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox tbFilePath;
private System.Windows.Forms.Button btnSelect;
private System.Windows.Forms.NotifyIcon niIcon;
private System.Windows.Forms.OpenFileDialog ofdFile;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.CheckedListBox clbDisk;
private System.Windows.Forms.Label label3;
private const int Removable = 2;
private const int LocalDisk = 3;
private const int Network = 4;
private const int CD = 5;
private System.Windows.Forms.TextBox tbFilter;
private string[] filterList;
private System.Windows.Forms.Button btnStop;
private Thread th;
private int j=0;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// 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.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.btnSearch = new System.Windows.Forms.Button();
this.tbInfo = new System.Windows.Forms.TextBox();
this.sbStatus = new System.Windows.Forms.StatusBar();
this.tbFilter = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.tbFilePath = new System.Windows.Forms.TextBox();
this.btnSelect = new System.Windows.Forms.Button();
this.niIcon = new System.Windows.Forms.NotifyIcon(this.components);
this.ofdFile = new System.Windows.Forms.OpenFileDialog();
this.clbDisk = new System.Windows.Forms.CheckedListBox();
this.label3 = new System.Windows.Forms.Label();
this.btnStop = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(184, 87);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 0;
this.label1.Text = "过虑条件:";
//
// btnSearch
//
this.btnSearch.Location = new System.Drawing.Point(520, 87);
this.btnSearch.Name = "btnSearch";
this.btnSearch.TabIndex = 1;
this.btnSearch.Text = "归档";
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// tbInfo
//
this.tbInfo.Location = new System.Drawing.Point(8, 192);
this.tbInfo.Multiline = true;
this.tbInfo.Name = "tbInfo";
this.tbInfo.ReadOnly = true;
this.tbInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.tbInfo.Size = new System.Drawing.Size(608, 96);
this.tbInfo.TabIndex = 2;
this.tbInfo.Text = "";
//
// sbStatus
//
this.sbStatus.Location = new System.Drawing.Point(0, 312);
this.sbStatus.Name = "sbStatus";
this.sbStatus.Size = new System.Drawing.Size(624, 22);
this.sbStatus.TabIndex = 3;
//
// tbFilter
//
this.tbFilter.Location = new System.Drawing.Point(248, 88);
this.tbFilter.Name = "tbFilter";
this.tbFilter.Size = new System.Drawing.Size(248, 21);
this.tbFilter.TabIndex = 4;
this.tbFilter.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(192, 32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 5;
this.label2.Text = "归档到:";
//
// tbFilePath
//
this.tbFilePath.Location = new System.Drawing.Point(248, 32);
this.tbFilePath.Name = "tbFilePath";
this.tbFilePath.Size = new System.Drawing.Size(248, 21);
this.tbFilePath.TabIndex = 6;
this.tbFilePath.Text = "";
//
// btnSelect
//
this.btnSelect.Location = new System.Drawing.Point(520, 32);
this.btnSelect.Name = "btnSelect";
this.btnSelect.TabIndex = 7;
this.btnSelect.Text = "选择...";
this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
//
// niIcon
//
this.niIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("niIcon.Icon")));
this.niIcon.Text = "";
this.niIcon.Visible = true;
this.niIcon.DoubleClick += new System.EventHandler(this.niIcon_DoubleClick);
//
// ofdFile
//
this.ofdFile.Filter = "文本文件|*.txt|xml文件|*.xml";
//
// clbDisk
//
this.clbDisk.CheckOnClick = true;
this.clbDisk.Location = new System.Drawing.Point(24, 56);
this.clbDisk.MultiColumn = true;
this.clbDisk.Name = "clbDisk";
this.clbDisk.Size = new System.Drawing.Size(120, 116);
this.clbDisk.TabIndex = 8;
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 32);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(128, 16);
this.label3.TabIndex = 9;
this.label3.Text = "选择要扫描的驱动器";
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(520, 136);
this.btnStop.Name = "btnStop";
this.btnStop.TabIndex = 10;
this.btnStop.Text = "停止";
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(624, 334);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.label3);
this.Controls.Add(this.clbDisk);
this.Controls.Add(this.btnSelect);
this.Controls.Add(this.tbFilePath);
this.Controls.Add(this.label2);
this.Controls.Add(this.tbFilter);
this.Controls.Add(this.sbStatus);
this.Controls.Add(this.tbInfo);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "归档硬盘文件";
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
niIcon.Visible=false;
InfoAdd("扫描信息...");
InfoAdd("------------------------------");
StatuAdd("正在扫描...");
ShowDisk();
StatuAdd("扫描完毕");
InfoAdd("-----------------------------");
}
private void InfoAdd(string info)
{
tbInfo.Text+=info+"/r/n";
}
private void StatuAdd(string statu)
{
sbStatus.Text=statu;
}
private void ShowDisk()
{
string str=string.Empty;
ManagementObjectCollection queryCollection = GetDrives();
foreach(ManagementObject obj in queryCollection)
{
switch (int.Parse(obj["DriveType"].ToString()))
{
case Removable: //removable drives
str="移动硬盘";
break;
case LocalDisk: //Local drives
str="本地硬盘";
break;
case CD: //CD rom drives
str="光驱";
break;
case Network: //Network drives
str="网络硬盘";
break;
default: //defalut to folder
str="未知驱动器";
break;
}
clbDisk.Items.Add(obj["Name"].ToString());
InfoAdd(obj["Name"].ToString()+" "+str);
}
}
private ManagementObjectCollection GetDrives()
{
//get drive collection
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();
//InfoAdd("共有"+queryCollection.Count.ToString()+"个逻辑驱动器");
return queryCollection;
}
private void btnSelect_Click(object sender, System.EventArgs e)
{
if(ofdFile.ShowDialog()==DialogResult.Cancel)
return;
tbFilePath.Text=ofdFile.FileName;
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
//bool listItem=true,diskItem=true;
if(tbFilter.Text.Trim()==String.Empty)
{
//listItem=false;
//if(MessageBox.Show("没有设置过虑条件,程序将提取所有的文件!/r/n继续吗?","提示",MessageBoxButtons.OKCancel)==DialogResult.Cancel)
MessageBox.Show("请设置过虑条件!如/".TXT,.EXE/"","提示");
return;
}
else
{
filterList=tbFilter.Text.Split(',');
}
th=new Thread(new ThreadStart(BeginSearch));
th.Start();
}
private void BeginSearch()
{
InfoAdd("扫描"+clbDisk.CheckedItems.Count.ToString()+"个驱动器../r/n--------------------------------");
for(int i=0;i<clbDisk.CheckedItems.Count;i++)
{
InfoAdd("开始扫描 "+clbDisk.CheckedItems[i].ToString()+" .../r/n--------------------------------");
Log(clbDisk.CheckedItems[i].ToString());
InfoAdd("结束扫描 "+clbDisk.CheckedItems[i].ToString()+" .../r/n--------------------------------");
}
InfoAdd("扫描到 "+j.ToString()+" 个文件");
}
private void Log(string path)
{
try
{
if(!Directory.Exists(path))
{
return;
}
StatuAdd("正在扫描 "+path+" ...");
DirectoryInfo dir=new DirectoryInfo(path);
DirectoryInfo[] dirs=dir.GetDirectories();
FileInfo[] files=dir.GetFiles();
StreamWriter writer=new StreamWriter(tbFilePath.Text);
foreach(FileInfo info in files)
{
for(int i=0;i<filterList.Length;i++)
{
if(info.Extension.Trim().ToLower()==filterList[i].Trim().ToLower())
{
writer.WriteLine(info.FullName);
InfoAdd(info.FullName);
j++;
}
}
}
writer.Flush();
writer.Close();
foreach(DirectoryInfo info in dirs)
{
Log(info.FullName);
}
}
catch(Exception ex)
{
InfoAdd("扫描异常:"+ex.Message);
//StatuAdd("扫描因为出现异常而结束");
}
}
private void btnStop_Click(object sender, System.EventArgs e)
{
if(th.IsAlive)
{
th.Abort();
}
InfoAdd("扫描到 "+j.ToString()+" 个文件");
StatuAdd("停止扫描");
}
private void niIcon_DoubleClick(object sender, System.EventArgs e)
{
this.Visible=true;
this.WindowState=System.Windows.Forms.FormWindowState.Normal;
this.niIcon.Visible=false;
}
private void Form1_SizeChanged(object sender, System.EventArgs e)
{
if(this.WindowState==System.Windows.Forms.FormWindowState.Minimized)
{
this.Hide();
this.niIcon.Visible=true;
}
}
}
}