コード例 #1
0
 /// <summary>
 /// 启用控件
 /// </summary>
 /// <param name="s"></param>
 private void EnableControl(object s)
 {
     ExtentionClass.EnableControl(s);
 }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: xztl/HostsManageTool
        /// <summary>
        /// 应用hosts文件(仅用户)点击
        /// </summary>
        private void btnApplyOnlyUser_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                try
                {
                    LblApplyMsgMessage("获取用户hosts配置...");
                    var dicUser = UserHostManager.Instance.GetUserDictionary();

                    LblApplyMsgMessage("读取远程hosts配置缓存...");
                    var dicRemote = HostsSourceManager.Instance.ReadRemoteDicBak();

                    LblApplyMsgMessage("混合hosts配置...");
                    foreach (KeyValuePair <string, string> pair in dicRemote ?? new Dictionary <string, string>())
                    {
                        if (!dicUser.ContainsKey(pair.Key))
                        {
                            dicUser.Add(pair.Key, pair.Value);
                        }
                    }
                    dicUser.Remove("localhost");
                    dicUser.Remove("broadcasthost");
                    var listDefault = new List <string>()
                    {
                        "127.0.0.1\tlocalhost",
                        "255.255.255.255\tbroadcasthost",
                        "::1\tlocalhost",
                        "fe80::1%lo0\tlocalhost"
                    };
                    listDefault.AddRange(dicUser.Select(pair => $"{pair.Value}\t{pair.Key}"));

                    LblApplyMsgMessage("备份Hosts文件...");

                    if (File.Exists(@"C:\windows\system32\drivers\etc\hosts"))
                    {
                        File.Copy(@"C:\windows\system32\drivers\etc\hosts",
                                  ExtentionClass.ApplicationPath + "hosts_" + DateTime.Now.ToString("yyyyMMddhhmmss_") + ExtentionClass.GetRandomStringLength10() + ".bak");
                        File.Delete(@"C:\windows\system32\drivers\etc\hosts");
                    }
                    var files = Directory.GetFiles(ExtentionClass.ApplicationPath)
                                .Where(d => Regex.IsMatch(d, @"hosts_[0-9]{14}_[A-Z0-9]{10}")).ToList();
                    if (files.Count > 5)
                    {
                        files = files.OrderByDescending(d => d).Skip(5).ToList();
                        foreach (var file in files)
                        {
                            File.Delete(file);
                        }
                    }

                    LblApplyMsgMessage("创建新的Hosts文件...");
                    File.WriteAllLines(@"C:\windows\system32\drivers\etc\hosts", listDefault);

                    LblApplyMsgMessage("完成...");
                }
                catch (HostSourceFalseException ex)
                {
                    Message("下载Hosts时失败:" + ex.RequestUrl + "  " + ex.Message);
                }
                catch (IOException ex)
                {
                    Message(ex.Message);
                }
                EnableControl(sender);
            })
            {
                IsBackground = true
            }
            .Start();
        }