谷歌chrome更新服务器hosts

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TravelRepublic.DnsClient;
using TravelRepublic.DnsClient.Records;
using System.Windows;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace Chrome更新服务器Hosts
{
    internal class Program
    {
        static void Main(string[] args)
        {
			Console.Title = "设置Chrome更新服务器地址 by 福建-兮";
			Console.SetWindowSize(70, 10);
			Console.SetBufferSize(70, 10);
			IntPtr consoleWindow = Program.GetConsoleWindow();
			Program.RECT rect;
			Program.GetWindowRect(consoleWindow, out rect);
			Screen screen = Screen.FromPoint(new Point(rect.left, rect.top));
			int x = screen.WorkingArea.Left + (screen.WorkingArea.Width - (rect.right - rect.left)) / 2;
			int y = screen.WorkingArea.Top + (screen.WorkingArea.Height - (rect.bottom - rect.top)) / 2;
			Program.MoveWindow(consoleWindow, x, y, rect.right - rect.left, rect.bottom - rect.top, true);
			Program.GetChromeUpdateServerIP("tools.google.com");//"tools.google.com"
			Console.ReadKey();
		}
		private static void GetChromeUpdateServerIP(string domain)
		{
			Console.Write("请输入解析DNS地址(默认为:114.114.114.114):");
			string text = Console.ReadLine();
			bool flag = string.IsNullOrEmpty(text);
			if (flag)
			{
				text = "114.114.114.114";
			}
			IDnsClient dnsClient = new DnsClientBuilder().WithDnsServer(new IPEndPoint(IPAddress.Parse(text), 53)).Build();
			try
			{
				Response response = dnsClient.Query(domain, NsType.A, NsClass.INET, ProtocolType.Udp);
				ARecord arecord = (ARecord)response.Answers[2];
				Console.Write("使用DNS:");
				Console.ForegroundColor = ConsoleColor.DarkGreen;
				Console.Write(text);
				Console.ResetColor();
				Console.Write("解析chrome更新服务器地址");
				Console.WriteLine();
				Console.Write("chrome更新服务器地址为:");
				Console.ForegroundColor = ConsoleColor.DarkGreen;
				Console.Write(arecord.Address);
				Console.ResetColor();
				Console.WriteLine();
				Console.WriteLine();
				Console.ForegroundColor = ConsoleColor.Magenta;
				Console.WriteLine("开始修改host");
				Console.ResetColor();
				Console.Write(arecord.Address);
				Console.WriteLine();
				Process process = new Process();
				process.StartInfo.FileName = "cmd.exe";
				process.StartInfo.UseShellExecute = false;
				process.StartInfo.RedirectStandardInput = true;
				process.StartInfo.RedirectStandardOutput = true;
				process.StartInfo.RedirectStandardError = true;
				process.StartInfo.CreateNoWindow = true;
				process.Start();
				process.StandardInput.WriteLine("attrib -r -a -s -h %windir%\\system32\\drivers\\etc\\hosts & exit");
				//process.StandardInput.AutoFlush= true;
                //string outstr = process.StandardOutput.ReadToEnd();
				process.WaitForExit();
				process.Close();
				string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers\\etc\\hosts");
				List<string> list = File.ReadAllLines(path).ToList<string>();
				list.RemoveAll((string z) => z.Contains(domain));
				foreach (string text2 in list.ToArray())
				{
					bool flag2 = text2.Contains(domain);
					if (flag2)
					{
						list.Remove(text2);
					}
				}
				list.Add(string.Format("{0}  {domain}", arecord.Address));//tools.google.com
				//list.Add(string.Format("{0}  dl.google.com", arecord.Address));
				File.WriteAllLines(path, list.ToArray());
				Console.ForegroundColor = ConsoleColor.Blue;
				Console.WriteLine("host修改完成");
				Console.ResetColor();
				Console.ReadLine();
			}
			catch (Exception ex)
			{
                System.Windows.MessageBox.Show(ex.StackTrace, "错误", MessageBoxButton.OK, MessageBoxImage.Hand);
			}
		}
		private static void SetConsolePosition()
		{
			IntPtr hWin = GetConsoleWindow();
			RECT rc;
			GetWindowRect(hWin, out rc);
			Screen scr = Screen.FromPoint(new Point(rc.left, rc.top));
			int x = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2;
			int y = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2;
			MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, true);
		}
		private struct RECT { public int left, top, right, bottom; }

		[DllImport("kernel32.dll", SetLastError = true)]
		private static extern IntPtr GetConsoleWindow();
		[DllImport("user32.dll", SetLastError = true)]
		private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc);
		[DllImport("user32.dll", SetLastError = true)]
		private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值