实现简单的 hosts 添加和删除工具

 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Linq;
 7using System.Text;
 8using System.Windows.Forms;
 9using System.IO;
10
11namespace Google.le
12{
13    public partial class Form1 : Form
14    {
15        public Form1()
16        {
17            InitializeComponent();
18        }

19        /// <summary>
20        /// 按钮事件,检查hosts 中是否有 相关的条目
21        /// </summary>
22        /// <param name="sender"></param>
23        /// <param name="e"></param>

24        private void button1_Click(object sender, EventArgs e)
25        {
26            string fileName = @"C:\Windows\System32\drivers\etc\hosts";
27            if (File.Exists(fileName))
28            {
29                MessageBox.Show("file exists");
30             //读文件stream
31                StreamReader stream = null;
32               //保存文件定义的字节流  
33                StreamWriter streamWriter = null;
34                List <string>ls = new List<string>();
35
36                try
37                {
38                    //打开hosts
39                    stream = new StreamReader(fileName);
40                    // a 标记是否有相关条目
41                    bool a = false;
42                    string strtemp="";
43                    string temp;
44                    while ((temp=stream.ReadLine())!=null{
45                        
46                       //相关条目名称 这里以Goog。le为例
47                        if (temp.StartsWith("#Goog.le")) {
48                            a = true;
49                            stream.ReadLine();
50                            stream.ReadLine();
51                        }
else {
52                           
53                         strtemp+=temp+Environment.NewLine;
54                        }

55
56                    }

57
58                    if (a) {
59                        MessageBox.Show("删除完毕");
60                    }

61                    else {
62                        strtemp += "#Goog.le"+ Environment.NewLine+"203.208.37.99 goog.le"+Environment.NewLine+"203.208.37.99 webcache.googleusercontent.com"+Environment.NewLine;
63                       
64                        
65                        MessageBox.Show("已经添加");
66                        
67                    }

68                    richTextBox1.Text = strtemp;
69                    stream.Close();
70                    streamWriter = new StreamWriter(fileName);
71                    
72                    streamWriter.Write(strtemp);
73                    streamWriter.Flush();
74                    streamWriter.Close();
75                }

76                catch (IOException){
77                
78                }

79
80            }

81            else
82            {
83                MessageBox.Show("file not found");
84            }

85        }

86    }

87}

88
关于用程序修改 hosts 这里做了简单的版本。这里用一种取巧的方式,并没有用正则表达式匹配。(虽说正则表达式的效果会好一点)以后有机会做一个正则表达式的。这个小程序正好是我的一个朋友需要的小工具。临时凑合了一个。

----------------以下是hosts的相关格式------此程序只适用于以下格式的hosts

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

#127.0.0.1         localhost                              

 218.11.132.81     f1.xiami.net                             #f3.xiami.net
#Goog.le
203.208.37.99 goog.le
203.208.37.99 webcache.googleusercontent.com

——————————————————————————————————————————————————————————
最后贴上一个我朋友的博客http://sneezry.com/ 里面有相关google的中国稳定的访问方式一文值得推荐

posted on 2011-02-21 00:01 scorpio小蝎 阅读(1060) 评论(0)  编辑  收藏 所属分类: C#


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年2月>
303112345
6789101112
13141516171819
20212223242526
272812345
6789101112

导航

统计

常用链接

留言簿

随笔分类

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜