⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 实现对硬盘文件的搜索..不过功能简单
💻 CS
字号:
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.IsolatedStorage;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace 搜索硬盘
{
    public partial class Form1 : Form
    {
        string[] dir = null;
        string[] dirs = null;
        bool buf = false;
        string path = "";
        FileInfo fileinfo = null;
        Thread t;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "")
            {
                return;
            }
            this.listView1.Items.Clear();
            button3.Enabled = true;
            foreach (Control control in groupBox1.Controls)
            {
                if (control!=button3)
                {
                    control.Enabled = false;
                }
            }
            t = new Thread(new ThreadStart(print));
            t.Start();
            timer1.Enabled = true;
        }

        private void print()
        {
            if (buf == false)
            {
                path = textBox1.Text;
            }
            buf = true;
            try
            {
                 dir  = Directory.GetDirectories(path);      //获取指定路径所有的文件夹
                 dirs = Directory.GetFiles(path);           //获取文件夹下所有的文件
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            foreach(string file in dirs)
            {
                try
                {
                    fileinfo = new FileInfo(file);
                    Invoke(new EventHandler(printMeg));
                    Thread.Sleep(10);
                }
                catch (Exception eer)
                {
                    MessageBox.Show(eer.Message);
                }
                
            }
            foreach (string f in dir)
            {
                path = f;
                print();  
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.ShowDialog();
            this.textBox1.Text = this.folderBrowserDialog1.SelectedPath.ToString();
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string FilePath = this.listView1.SelectedItems[0].SubItems[1].Text;
            Process p = Process.Start(FilePath);
        }

        private void printMeg(object sender, EventArgs e)
        {
            if (textBox2.Text == "*.*" || textBox2.Text == ".*")
            {
                string strFile = fileinfo.Name;
                ListViewItem item = this.listView1.Items.Add(strFile);
                item.SubItems.Add(fileinfo.DirectoryName);
                item.SubItems.Add(fileinfo.Length.ToString() + "字节");
                item.SubItems.Add(fileinfo.CreationTime.ToString());
            }
            if (fileinfo.Extension.Equals(textBox2.Text) || fileinfo.Name.Equals(textBox2.Text) || "*"+fileinfo.Extension==textBox2.Text)
            {
                string strFile = fileinfo.Name;
                ListViewItem item = this.listView1.Items.Add(strFile);
                item.SubItems.Add(fileinfo.DirectoryName);
                item.SubItems.Add(fileinfo.Length.ToString() + "字节");
                item.SubItems.Add(fileinfo.CreationTime.ToString());
            }
            this.label1.Text = fileinfo.DirectoryName.ToString();
            this.label1.Refresh();            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            t.Abort();
            foreach (Control control in groupBox1.Controls)
            {
                if (control.Enabled == false)
                {
                    control.Enabled = true;
                }
            }
            button3.Enabled = false;
            buf = false;
            timer1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (t.IsAlive==false)
            {
                label1.Text = "搜索完毕!";
                button3_Click(null, null);
            }
        }

        private void 打开文件OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string FilePath = this.listView1.SelectedItems[0].SubItems[1].Text + "\\" + this.listView1.SelectedItems[0].SubItems[0].Text;
            Process p = Process.Start(FilePath);
        }

        private void 打开文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string FilePath = this.listView1.SelectedItems[0].SubItems[1].Text;
            Process p = Process.Start(FilePath);
        }

        private void 刷新EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.Refresh();
        }

        private void 停止QToolStripMenuItem_Click(object sender, EventArgs e)
        {
            t.Abort();
            foreach (Control control in groupBox1.Controls)
            {
                if (control.Enabled == false)
                {
                    control.Enabled = true;
                }
            }
            button3.Enabled = false;
            buf = false;
            timer1.Enabled = false;
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (t.IsAlive == true)
                {
                    if (MessageBox.Show("搜索尚未结束,是否要退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
                    {
                        t.Abort();
                        Application.Exit();
                    }
                    else
                        return;
                }
                else
                    Application.Exit();
            }
            catch
            {
                Application.Exit();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -