C#文件查找(按内容、文件名称查找)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Collections;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Search
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region函数
///
///选中的文件夹
///
private DirectoryInfo _dirSelected;
///
///检索关键字
///
private string _strKeyWord;
///
///存储文件的DataTable
///
private DataTable _dtFileInfo;
///
///可查找的后缀名
///
private Hashtable _htExtension;
///
///检索方式:1 按内容检索 2按文件名称检索
///
private int _iQueryType = 1;
private bool _bFlagRunCompleted = true;
///
///是否区分大小写
///
private bool _bFlagIsUpLower = true;
private const int _GB = 1024 * 1024 * 1024;//定义GB的计算常量
private const int _MB = 1024 * 1024;//定义MB的计算常量
private const int _KB = 1024;//定义KB的计算常量
private delegate void dlgShowSearchFileName(string strMsg);
private delegate void dlglBindDataTableToDataGridView();
#endregion
#region方法
#region跨线程访问控件
///
///跨线程访问控件:显示检索文件名
///
///
private void AccessToControl_ShowSearchFileName(string strMsg)
{
if (this.InvokeRequired)
{
dlgShowSearchFileName dlgShowMsg = new
dlgShowSearchFileName(AccessToControl_ShowSearchFileName);
this.lblSearchMsg.Invoke(dlgShowMsg, new object[] { strMsg }); }
else
{
this.lblSearchMsg.Text = string.Format("正在检索 {0}", strMsg); }
}
private void AccessToControl_BindDataTableToDataGridView()
{
this.Invoke(new dlglBindDataTableToDataGridView(delegate()
{
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = this._dtFileInfo;
}));
}
#endregion
#region搜索
///
///检索
///
private void Search()
{
ArrayList alFile = new ArrayList();
SearchFiles(this._dirSelected, ref alFile);
}
///
///遍历文件夹下的所有文件
///
///
///
private void SearchFiles(FileSystemInfo info, ref ArrayList alFileInfo) {
if (!this._bFlagRunCompleted)
{
if (!info.Exists) return;
DirectoryInfo dir = info as DirectoryInfo;