版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、【原创】c# 文件全盘搜索 多线程 vs2005 winform项目中用到的文件搜索功能,可扩展,多线程并行搜索,希望能帮到有需要的朋友们!注:下载后,按目录分别创建文件及类,这个示例中用的是Form1,已经过测试,速度还是不错的!当然如果你不会创建文件和类,请绕道!转载请注明出处!第一Form1.Designer.csnamespace 多线程文件搜索 partial class Form1 / <summary> / 必需的设计器变量。 / </summary> private System.ComponentModel.IContainer components
2、= null; / <summary> / 清理所有正在使用的资源。 / </summary> / <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) if (disposing && (components != null) components.Dispose(); base.Dispose(disposing); #region Windows
3、 窗体设计器生成的代码 / <summary> / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / </summary> private void InitializeComponent() this.label1 = new System.Windows.Forms.Label(); this.GridSearch = new System.Windows.Forms.DataGridView(); this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new Sy
4、stem.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); (System.ComponentModel.ISupportInitialize)(this.GridSearch).BeginInit(); this.SuspendLayout(); / / label1 / this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(10, 8); this.label1.Name = "labe
5、l1" this.label1.Size = new System.Drawing.Size(101, 12); this.label1.TabIndex = 8; this.label1.Text = "文件名不带扩展名" / / GridSearch / this.GridSearch.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.GridSearch.Location = new System.
6、Drawing.Point(12, 60); this.GridSearch.Name = "GridSearch" this.GridSearch.RowTemplate.Height = 23; this.GridSearch.Size = new System.Drawing.Size(407, 243); this.GridSearch.TabIndex = 7; / / button1 / this.button1.Location = new System.Drawing.Point(425, 23); this.button1.Name = "but
7、ton1" this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 6; this.button1.Text = "Seach" this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); / / textBox1 / this.textBox1.Location = new System.Drawing.
8、Point(12, 23); this.textBox1.Name = "textBox1" this.textBox1.Size = new System.Drawing.Size(407, 21); this.textBox1.TabIndex = 5; / / label2 / this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 316); this.label2.Name = "label2" this.label2.Size = new
9、 System.Drawing.Size(41, 12); this.label2.TabIndex = 9; this.label2.Text = "label2" / / Form1 / this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(518, 331); this.Controls.Add
10、(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.GridSearch); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1" this.Text = "Form1" (System.ComponentModel.ISupportInitialize)(this.GridSearch).EndInit(); this.ResumeLayout(f
11、alse); this.PerformLayout(); #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.DataGridView GridSearch; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label2; 第二Form1.csusing System;using
12、 System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace 多线程文件搜索 / <summary> /功能描述 : 测试 /开发者 : Mr King /建立时间 : 2009-2-1 13:28:58 /修订描述 : /进度描述 : /版本号 : 1.0 /最后修改时间: 2009-2-1 16:28:58 /
13、/Function Description : Test /Developer : /Builded Date: 2009-2-1 13:28:58 /Revision Description : /Progress Description : /Version Number : 1.0 /Last Modify Date : 2009-2-1 16:28:58 public partial class Form1 : Form #region/字段与属性 string strFileName; public string StrFileNameget return strFileName;
14、set strFileName = value; delegate void Temp(string fileName); delegate void ShowProgressDelegate(string fileName, string filePath, IList<IlsitFileFindModleClass> objIlist); IList<IlsitFileFindModleClass> objIlist; #endregion #region/初始化与构造函数 public Form1() InitializeComponent(); #endregi
15、on #region/测试方法 / <summary> / 全盘搜索 / </summary> public void GetFoldersPath() if (GridSearch.InvokeRequired = false) else objIlist = new List<IlsitFileFindModleClass>(); string retStr = null; DriveInfo allDrives = DriveInfo.GetDrives();/检索计算机上的所有逻辑驱动器的驱动器名称 foreach (DriveInfo driPat
16、h in allDrives)/循环输出各分区的信息 FileSeach(driPath.Name); / <summary> / 路径搜索 / </summary> / <param name="root"></param> public void GetFoldersPath(string root) if (GridSearch.InvokeRequired = false) else objIlist = new List<IlsitFileFindModleClass>(); FileSeach(root
17、); #endregion #region/私有方法 / <summary> / 文件搜索 / </summary> / <param name="root">搜索地址</param> public void FileSeach(string root) string strFileNameTemp = StrFileName; Stack<string> dirs = new Stack<string>(20); if (!System.IO.Directory.Exists(root) return
18、; dirs.Push(root); while (dirs.Count > 0) string currentDir = dirs.Pop(); string subDirs; try subDirs = System.IO.Directory.GetDirectories(currentDir); catch (UnauthorizedAccessException e) continue; catch (System.IO.DirectoryNotFoundException e) continue; Temp temp=new Temp(Testll); label2.Invok
19、e(temp, new object currentDir ); try string filesTemp = System.IO.Directory.GetFiles(currentDir, StrFileName + ".*"); if (filesTemp.Length > 0) objIlist.Add(new IlsitFileFindModleClass(strFileNameTemp, currentDir + "" + strFileNameTemp + ".*"); ShowProgressDelegate o
20、bjTemp = new ShowProgressDelegate(test); BeginInvoke(objTemp, new object strFileNameTemp, currentDir + "" + strFileNameTemp + ".*", objIlist ); continue; catch (UnauthorizedAccessException e) continue; catch (System.IO.DirectoryNotFoundException e) continue; foreach (string str i
21、n subDirs) dirs.Push(str); / <summary> / 同步更新 / </summary> / <param name="Path"></param> void Testll(string Path) label2.Text = Path; label2.Update(); / <summary> / UI异步代理 / </summary> / <param name="fileName"></param> / <param n
22、ame="filePath"></param> / <param name="objIlist"></param> void test(string fileName, string filePath, IList<IlsitFileFindModleClass> objIlist) GridSearch.DataSource = null; GridSearch.DataSource = objIlist; GridSearch.Columns0.HeaderText = "名称"
23、; GridSearch.Columns1.HeaderText = "地址" #endregion #region/窗体事件 private void button1_Click(object sender, EventArgs e) StrFileName = textBox1.Text; MethodInvoker mi = new MethodInvoker(GetFoldersPath); mi.BeginInvoke(null, null); #endregion 第三Form1<?xml version="1.0" encoding=
24、"utf-8"?><root> <!- Microsoft ResX Schema Version 2.0 The primary goals of this format is to allow a simple XML format that is mostly human readable. The generation and parsing of the various data types are done through the TypeConverter classes associated with the data types.
25、Example: . headers & schema . <resheader name="resmimetype">text/microsoft-resx</resheader> <resheader name="version">2.0</resheader> <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, .</resheader>
26、; <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, .</resheader> <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data> <data name="Color1"
27、 type="System.Drawing.Color, System.Drawing">Blue</data> <data name="Bitmap1" mimetype="application/x-.object.binary.base64"> <value>base64 mime encoded serialized .NET Framework object</value> </data> <data name="Icon1" typ
28、e="System.Drawing.Icon, System.Drawing" mimetype="application/x-.object.bytearray.base64"> <value>base64 mime encoded string representing a byte array form of the .NET Framework object</value> <comment>This is a comment</comment> </data> There are
29、 any number of "resheader" rows that contain simple name/value pairs. Each data row contains a name, and value. The row also contains a type or mimetype. Type corresponds to a .NET class that support text/value conversion through the TypeConverter architecture. Classes that don't suppo
30、rt this are serialized and stored with the mimetype set. The mimetype is used for serialized objects, and tells the ResXResourceReader how to depersist the object. This is currently not extensible. For a given mimetype the value must be set accordingly: Note - application/x-.object.binary.base64 is
31、the format that the ResXResourceWriter will generate, however the reader can read any of the formats listed below. mimetype: application/x-.object.binary.base64 value : The object must be serialized with : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter : and then encoded with base64
32、encoding. mimetype: application/x-.object.soap.base64 value : The object must be serialized with : System.Runtime.Serialization.Formatters.Soap.SoapFormatter : and then encoded with base64 encoding. mimetype: application/x-.object.bytearray.base64 value : The object must be serialized into a byte ar
33、ray : using a System.ComponentModel.TypeConverter : and then encoded with base64 encoding. -> <xsd:schema id="root" xmlns="" xmlns:xsd="/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xsd:import namespace=&quo
34、t;/XML/1998/namespace" /> <xsd:element name="root" msdata:IsDataSet="true"> <xsd:complexType> <xsd:choice maxOccurs="unbounded"> <xsd:element name="metadata"> <xsd:complexType> <xsd:sequence> <xsd:
35、element name="value" type="xsd:string" minOccurs="0" /> </xsd:sequence> <xsd:attribute name="name" use="required" type="xsd:string" /> <xsd:attribute name="type" type="xsd:string" /> <xsd:attribut
36、e name="mimetype" type="xsd:string" /> <xsd:attribute ref="xml:space" /> </xsd:complexType> </xsd:element> <xsd:element name="assembly"> <xsd:complexType> <xsd:attribute name="alias" type="xsd:string" /&g
37、t; <xsd:attribute name="name" type="xsd:string" /> </xsd:complexType> </xsd:element> <xsd:element name="data"> <xsd:complexType> <xsd:sequence> <xsd:element name="value" type="xsd:string" minOccurs="0"
38、 msdata:Ordinal="1" /> <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> </xsd:sequence> <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="
39、1" /> <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> <xsd:attribute ref="xml:space" /> </xsd:complexType> &
40、lt;/xsd:element> <xsd:element name="resheader"> <xsd:complexType> <xsd:sequence> <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> </xsd:sequence> <xsd:attribute name="name" type="xsd:string" use="required" /> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema> <resheader name="resmimetype"> <valu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 进口产品合作合同范本
- 租场地存放合同协议书
- 2025年高中三年级化学上学期专项练习
- 酒店包房租赁合同范本
- 物业管理业务合同范本
- 社区团购物流合同范本
- 眼科视力矫正合同范本
- 监理公司出资合同范本
- 行政合同拆迁补偿协议
- 华师大版七年级上册1 有理数的乘法法则教学设计
- ESG基础知识培训课件
- 足球队管理规章制度与队员行为准则
- 【课件】圆锥曲线的光学性质探究+课件-2024-2025学年高二上学期数学人教A版(2019)选择性必修第一册
- 河道整治与生态修复工程方案
- 药学三基考试试题(带答案)
- 彩妆师的培训课件
- 2025年教师入编考试教育公共基础知识题库及答案
- 2025年6月黑吉辽蒙高考地理真题完全解读
- 2025电化学储能电站技术监督规程第2部分:储能系统技术监督
- 社工服务培训课件
- 智能体技术和应用研究报告(2025年)
评论
0/150
提交评论