关闭

C#文件搜索实例

6259人阅读 评论(9) 收藏 举报

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;
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:324377次
    • 积分:4445
    • 等级:
    • 排名:第6930名
    • 原创:81篇
    • 转载:0篇
    • 译文:2篇
    • 评论:431条
    文章分类
    最新评论
    chenhui530新浪博客