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 = "未找到文件";
                }
            }
        }
    }
}

界面如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#开发,有一些常用的动态代码分析工具可以帮助您进行代码性能和行为分析。以下是一些常见的C#动态代码分析工具: 1. Profiler (性能分析器):性能分析器是一种常用的动态代码分析工具,可用于检测和识别应用程序的性能瓶颈。它可以帮助您找出代码的性能问题,并提供详细的性能报告和建议。一些常见的C#性能分析器包括JetBrains dotTrace、ANTS Performance Profiler和Visual Studio Profiler。 2. Memory Profiler (内存分析器):内存分析器用于检测和识别应用程序的内存泄漏和内存使用问题。它可以帮助您查找未释放的资源、不必要的对象创建和过度使用内存等问题。一些常见的C#内存分析器包括JetBrains dotMemory、ANTS Memory Profiler和Visual Studio Memory Profiler。 3. Code Coverage Tools (代码覆盖工具):代码覆盖工具可以帮助您确定测试哪些代码行被执行或覆盖到,以评估测试的充分性。它可以显示哪些部分的代码没有得到测试覆盖,从而帮助您改进测试策略。一些常见的C#代码覆盖工具包括JetBrains dotCover、OpenCover和NCover。 4. Debugging Tools (调试工具):调试工具是一种常见的动态代码分析工具,用于在运行时跟踪和调试代码。它可以帮助您识别和修复代码的错误和异常。Visual Studio是一种强大的C#调试工具,提供了丰富的调试功能。 这些工具可以与C#开发环境集成,帮助您分析代码性能、内存使用和行为。您可以根据您的需求选择适合的工具,并根据具体情况进行设置和配置。 希望这些信息对您有所帮助!如有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值