C#代码搜索工具

搜索本机所有C#代码(.cs结尾的文件),使用Task.Run在后台异步加载,本机搜索不到时会自动打开cn.bing.com网站去搜

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace CodeSearch
{
    //Created By Cryking 2015.6.2
    public partial class Form1 : Form
    {
        Dictionary<string, string> dt = new Dictionary<string, string>();
        public Form1()
        {
            InitializeComponent();
            tb2.ScrollBars = ScrollBars.Vertical;
            tb2.BackColor = Color.White;
            tb2.ForeColor = Color.Red;
        }

        void FileSearch()
        {
            int i = 0;
            string[] astr = Directory.GetLogicalDrives();

            foreach (string s in astr)
            {
                try
                {
                    string[] ast = Directory.GetFiles(s, "*.cs");
                    if (ast.Length == 0)
                        ast = Directory.GetDirectories(s);
                    else
                    {
                        foreach (string s2 in ast)
                        {
                            string t2 = s2.Substring(s2.LastIndexOf('\\') + 1);
                            AddToListBox(i, t2, s2);
                            i++;
                        }
                    }
                    foreach (string s1 in ast)
                    {
                        try
                        {
                           //这些目录不会有cs文件,过滤掉
                            if (s1.Contains(@"Program Files") || s1.Contains(@"C:\Windows")
                                || s1.Contains("System Volume Information")) continue;
                            string[] atmp = Directory.GetFiles(s1, "*.cs", SearchOption.AllDirectories);
                            if (atmp.Length != 0)
                            {
                                foreach (string s2 in atmp)
                                {
                                    string t2 = s2.Substring(s2.LastIndexOf('\\') + 1);
                                    AddToListBox(i, t2, s2);
                                    i++;
                                }
                            }
                        }
                        catch ()
                        {
                            continue;
                        }
                       UpdateLabText(string.Format(":.CS文件数({0})", i));
                    }
                }
                catch ()
                {
                    continue;
                }
            }
        }

        void UpdateLabText(string text)
        {
            Invoke(new Action(() =>
            {
                lab1.Text = text;
            }));
        }

        void AddToListBox(int i,string file,string path)
        {
            Invoke(new Action(() =>
            {
                lb_codefile.Items.Add(file);
                dt.Add(i.ToString() + file, path);
            }));
        }

        private void lb_codefile_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListBox lb1 = (ListBox)sender;
            //双击左边的代码文件列表,自动调用VS打开对应文件,需要设置把VS执行程序devenv.exe的路径加入到path环境变量
            System.Diagnostics.Process.Start("devenv.exe"
           , dt[lb1.SelectedIndex.ToString() + lb1.Text].ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tb1.Text)) return;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lb_codefile.Items.Count; i++)
            {
                string strfile=dt[i + lb_codefile.Items[i].ToString()].ToString();
                sb.Clear();
                using (StreamReader sr = new StreamReader(strfile, Encoding.Default))
                {
                    sb.Append(sr.ReadToEnd());
                    if (sb.ToString().Contains(tb1.Text))
                    {
                        tb2.Text = lb_codefile.Items[i].ToString() + "\r\n\r\n" + sb.ToString();
                        return;
                    }
                }
            }
            //本地搜索不到,去网上搜索
            System.Diagnostics.Process.Start(@"
	%programfiles%/Internet Explorer/IEXPLORE.EXE", @"http://cn.bing.com/search?q=" + tb1.Text);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           Task.Run(()=> FileSearch());
        }

        private void lb_codefile_MouseClick(object sender, MouseEventArgs e)
        {
            ListBox lb1 = (ListBox)sender;
            if (lb1.SelectedIndex > 0)
            {
                string strfile = dt[lb1.SelectedIndex.ToString() + lb1.Text].ToString();
                StringBuilder sb = new StringBuilder(5000);
                try
                {
                    using (StreamReader sr = new StreamReader(strfile, Encoding.Default))
                    {
                        sb.Append(sr.ReadToEnd());
                    }
                    tb2.Text = string.Format("文件路径{0}{1}{2}", strfile, Environment.NewLine, sb.ToString());
                }
                catch
                {
                    tb2.Text = "未找到文件";
                }
            }
        }
    }
}

界面如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值