意外发现百度在搜索结果页也自带了划词翻译,所以就有了这个小工具,支持即时输入即时翻译,自动识别语言翻译成中文,为中文的话翻译成英文。原理是获取http://sensearch.baidu.com/sensearch/selecttext?&q=XXX翻译结果页。结果页返回的json字符串,所以小工具引用了JSON.NET

有关JSON.NET里的备忘:

JObject.Parse(string s)//将字符串转换为json对象
JArray.Parse(string s)//数组字符串转换为包含json对象的数组


下载:http://pan.baidu.com/s/1gdfO6gb

微软官方昨天发布了一个 Visual Studio 扩展,Bing Code Search,可以方便的搜索代码并导入到你的项目中,例如如何将一个字符串 MD5 加密等等。搜索的结果来自 MSDN、StackOverflow 等开发社区。注意目前只支持 C# 哦。
bingcodesearch

功能:输入正则表达式即时匹配出结果

reg

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace RegexTool
{
    public partial class Form1 : Form
    {
        Regex reg;
        public Form1()
        {
            InitializeComponent();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
            richTextBox1.SelectionBackColor = Color.White;
            regIt();
        }
        private void regIt()
        {
            try
            {
                if (textBox1.Text == "")
                {
                    label1.Text = "";
                }
                else
                {
                    reg = new Regex(textBox1.Text);
                    MatchCollection mc = reg.Matches(@richTextBox1.Text);
                    int count = 0;
                    for (int i = 0; i < mc.Count; i++)
                    {
                        richTextBox1.Select(mc[i].Index, mc[i].Length);
                        if ((i + 2) % 2 == 0)
                        {
                            richTextBox1.SelectionBackColor = ColorTranslator.FromHtml("#FFF000");
                        }
                        else
                        {
                            richTextBox1.SelectionBackColor = ColorTranslator.FromHtml("#D2F854");
                        }
                        if (mc[i].Length != 0)
                        {
                            count++;
                        }
                    }
                    label1.Text = "结果:" + count + "个";
                }
            }
            catch (Exception ex)
            {
                label1.Text = "错误:" + ex.Message;
            }
        }
        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.V)
            {
                e.Handled = true;
                richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Text));
            }
        }
        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (textBox1.Text == "在此输入正则表达式")
            {
                textBox1.Clear();
            }
        }
        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "在此输入正则表达式";
            }
        }
    }
}

之前的自动更新 Smarthosts 到本地 hosts最后需要刷新 DNS 缓存,刷新 DNS 缓存首先想到的就是“ipconfig /flushdns” 命令,直接调用 cmd.exe 就可以了。不知道 .net 里是否提供了直接刷新 DNS 的方法,于是搜索了一下,很奇怪搜索“ C# 刷新 DNS 缓存”竟然找不到想要的结果,搜索“c# flush dns cache”一下就有答案了。方法大概有两种,一种就是调用 cmd.exe,一种就是调用系统 API:dnsapi.dll,其实 cmd 里方法也是调用 dnsapi.dll 实现的,为了不那么麻烦,这里就直接调用 cmd.exe 了。

using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "ipconfig /flushdns";
process.StartInfo = startInfo;
process.Start();
}

SmartHosts 是一个托管在 googlecode 上的项目,定期更新 hosts 文件,你懂的。为了方便自动更新,不用每次都打开网站,于是尝试用 c# 实现了一下。

实现原理

https://smarthosts.googlecode.com/svn/trunk/hosts 抓取数据,写入本地 hosts。

实现步骤

1.抓取数据

2.判断本地是否有 hosts 文件。没有则直接建立,并写入抓取下来的数据;有则读取

- 阅读剩余部分 -