搜索本机所有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 = "未找到文件";
}
}
}
}
}
界面如下: