当前位置: 首页 > 工具软件 > hosts > 使用案例 >

谷歌chrome更新服务器hosts

柯建业
2023-12-01
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);
	}
}

 类似资料: