C#文件搜索实例

原创 2007年10月02日 15:49:00

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace SearchExample
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        private const int WM_VSCROLL = 0x115;
        private const int SB_BOTTOM = 7;
        [DllImport("user32")]
        private extern static int SendMessage(int hWnd, int wMsg, int wParam, int lParam);

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(textPath.Text))
            {
                lvFiles.Items.Clear();
                cmdSearch.Enabled = false;
                textPath.Enabled = false;
                cmdBrowser.Enabled = false;
                ListViewItem item = new ListViewItem();
                int sum = 0;
                string[] rootFiles = Directory.GetFiles(textPath.Text);
                foreach (string rootfile in rootFiles)
                {
                    if (sum % 10 == 0) Application.DoEvents();
                    FileInfo fileInfo = new FileInfo(rootfile);
                    item = lvFiles.Items.Add(fileInfo.Name);
                    item.SubItems.AddRange(new string[] { rootfile, fileInfo.Length.ToString(), "文件" });  
                }
                SearchAllFiles(textPath.Text);
                cmdSearch.Enabled = true;
                textPath.Enabled = true;
                cmdBrowser.Enabled = true;
                SendMessage(lvFiles.Handle.ToInt32(), WM_VSCROLL, SB_BOTTOM, 0);
            }
            else
            {
                MessageBox.Show("请选择需要搜索的路径!!");
            }
        }

        private void SearchAllFiles(string strPath)
        {
            ListViewItem item = new ListViewItem();
            int sum = 0;
            string[] folders = Directory.GetDirectories(strPath);
            foreach(string folder in folders)
            {
                if (sum % 10 == 0) Application.DoEvents();
                sum += 1;
                DirectoryInfo folderInfo = new DirectoryInfo(folder);
                item = lvFiles.Items.Add(folderInfo.Name);
                item.SubItems.AddRange(new string[]{folder,"0","目录"});
                string[] files = Directory.GetFiles(folder);
                foreach (string file in files)
                {
                    if (sum % 10 == 0) Application.DoEvents();
                    sum += 1;
                    FileInfo fileInfo = new FileInfo(file);
                    item = lvFiles.Items.Add(fileInfo.Name);
                    item.SubItems.AddRange(new string[] { file, fileInfo.Length.ToString(), "文件" });
                }
                SearchAllFiles(folder);
            }
        }

        private void cmdFolderCount_Click(object sender, EventArgs e)
        {
            int sum=0;
            for (int i = 0; i < lvFiles.Items.Count; i++)
            {
                if (lvFiles.Items[i].SubItems[3].Text == "目录")
                {
                    sum += 1;
                }
            }
            MessageBox.Show("目录数为: " + sum.ToString());
        }

        private void cmdFileCount_Click(object sender, EventArgs e)
        {
            int sum = 0;
            for (int i = 0; i < lvFiles.Items.Count; i++)
            {
                if (lvFiles.Items[i].SubItems[3].Text == "文件")
                {
                    sum += 1;
                }
            }
            MessageBox.Show("文件数为: " + sum.ToString());
        }

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

        private void cmdBrowser_Click(object sender, EventArgs e)
        {
            folderBrowser.Description = "请选择需要搜索的文件路径!!";
            folderBrowser.ShowDialog();
            textPath.Text = folderBrowser.SelectedPath;
        }
    }
}

相关文章推荐

一个输入指定盘符和文件名检索文件存在路径的c#程序

0.1版本 using System; using System.IO; namespace FileSearch { class Program { public static void M...

C# 遍历文件夹及其子文件夹来查找某个文件

DirectoryInfo dir = new DirectoryInfo(@"D:\VS2012Project\测试用例\");             foreach (FileInfo fil...

用C#实现在Word文档中搜索文本

在word应用程序中搜索和替换文本是举手之劳的事情,通过word的对象模型,我们也可以使用编程方式来实现。   Word的对象模型有比较详细的帮助文档,放在office安装程序目录,office 20...

C#高级搜索文件实例

  • 2013年08月04日 12:16
  • 13KB
  • 下载

c#解析Json文件实例学习

JSON语法规则 数据在键值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 json简单来说就是javascript中的对象和数组,所以这联众结构就是对象和数组两种结构,通过这两种结构可以表示...

C#分块下载文件实例(二)

一、C#下载实例二 1、测试入口 /// /// 可指定cookie的方式下载 /// public static void Test2() { string url = "http:...

C# ListView实例:文件图标显示

主程序: 大图标: 列表: 详细信息: Form1.cs: public partial class Form1 : Form     {         FileInfoLi...

C#如何把类CLASS实例进行序列化及反序列化为XML文件

摘要:文章:C#如何把类CLASS实例进行序列化及反序列化为XML文件 摘要:C#如何把类Class实例进行序列化及反序列化为XML文件.NET2.0中可以很方便得把一个类序列化为XML文件,需要...
  • wyh0318
  • wyh0318
  • 2012年07月03日 09:42
  • 1456

C#封装的常用文件操作类实例

using System; using System.Text; using System.Web; using System.IO; namespace DotNet.Utilities { p...

Unity中C# 文件本地读取,本地保存等实例

using UnityEngine; using System.Collections; using System.IO; using System; public class FileReadWr...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#文件搜索实例
举报原因:
原因补充:

(最多只允许输入30个字)