HttpWebRequest第一次请求很慢
req.Proxy=null;
或者在app.config
里
<configuration>
<system.net>
<defaultProxy
enabled="false"
useDefaultCredentials="false" >
</defaultProxy>
</system.net>
</configuration>
req.Proxy=null;
或者在app.config
里
<configuration>
<system.net>
<defaultProxy
enabled="false"
useDefaultCredentials="false" >
</defaultProxy>
</system.net>
</configuration>
意外发现百度在搜索结果页也自带了划词翻译,所以就有了这个小工具,支持即时输入即时翻译,自动识别语言翻译成中文,为中文的话翻译成英文。原理是获取http://sensearch.baidu.com/sensearch/selecttext?&q=XXX
翻译结果页。结果页返回的json字符串,所以小工具引用了JSON.NET。
有关JSON.NET里的备忘:
JObject.Parse(string s)//将字符串转换为json对象
JArray.Parse(string s)//数组字符串转换为包含json对象的数组
一种打乱数组的方法
Random r = new Random();
for (int i = 0; i < paras.Count; i++)
{
int j = i + r.Next(paras.Count - i);
object t = paras[j];
paras[j] = paras[i];
paras[i] = t.ToString();
}
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();
}
添加新项>常规>应用程序清单文件
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
功能:输入正则表达式即时匹配出结果
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 文件。没有则直接建立,并写入抓取下来的数据;有则读取