{"id":441,"date":"2025-02-17T15:07:43","date_gmt":"2025-02-17T07:07:43","guid":{"rendered":"https:\/\/yuthk.com\/?p=441"},"modified":"2025-02-17T23:41:26","modified_gmt":"2025-02-17T15:41:26","slug":"%e4%bd%bf%e7%94%a8%e6%a8%a1%e6%8b%9f%e9%94%ae%e7%9b%98%e6%93%8d%e4%bd%9c%e7%9a%84%e6%96%b9%e5%bc%8f%e7%bb%95%e8%bf%87%e5%ae%89%e5%85%a8%e8%bd%af%e4%bb%b6%e5%af%b9hosts%e6%96%87%e4%bb%b6%e9%94%81","status":"publish","type":"post","link":"https:\/\/yuthk.com\/?p=441","title":{"rendered":"\u4f7f\u7528\u6a21\u62df\u952e\u76d8\u64cd\u4f5c\u7684\u65b9\u5f0f\u7ed5\u8fc7\u5b89\u5168\u8f6f\u4ef6\u5bf9Hosts\u6587\u4ef6\u9501\u5b9a\u7684\u4e00\u79cd\u89e3\u51b3\u529e\u6cd5"},"content":{"rendered":"\n<p>\u516c\u53f8\u7684\u5361\u5df4\u65af\u57fa\u5b89\u5168\u8f6f\u4ef6\u5bf9Hosts\u6587\u4ef6\u9501\u5b9a\u4e86\u3002\u4f7f\u7528\u4ee3\u7801\u521b\u5efa\u7684CMD\u8fdb\u7a0b\u4ea6\u6216\u662f\u76f4\u63a5\u64cd\u4f5cHosts\u6587\u4ef6\u90fd\u65e0\u6cd5\u64cd\u4f5c\uff0c\u4f46\u5bf9\u624b\u52a8\u6253\u5f00\u7684CMD\u5374\u80fd\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\uff0c\u6545\u60f3\u5230\u6b64\u529e\u6cd5<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06\u8f93\u5165\u6cd5\u9ed8\u8ba4\u4e3a\u82f1\u8bed<\/li>\n\n\n\n<li>\u964d\u4f4eUAC\u7b49\u7ea7\uff0c\u9632\u6b62UAC\u5f39\u7a97\u5e72\u6270<\/li>\n\n\n\n<li>\u6a21\u62df\u952e\u76d8\u64cd\u4f5c\u542f\u52a8CMD<\/li>\n\n\n\n<li>\u6a21\u62df\u624b\u52a8\u4f7f\u7528CMD\u4fee\u6539Hosts<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Diagnostics;\nusing System.Drawing;\nusing System.IO;\nusing System.Linq;\nusing System.Reflection;\nusing System.Runtime.InteropServices;\nusing System.Text;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\nusing Microsoft.Win32;\n\nnamespace \u5929\u9752\u8272\u7b49\u70df\u96e8.PDC\n{\n    public partial class PDCForm : Form\n    {\n        \/\/---------\u5207\u6362\u952e\u76d8\n        \/\/ \u58f0\u660e Windows API \u51fd\u6570\n        &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n        private static extern IntPtr ImmGetDefaultIMEWnd(IntPtr hWnd);\n\n        &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n        private static extern bool ImmIsIME(IntPtr hKL);\n\n        &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n        private static extern IntPtr ImmGetContext(IntPtr hWnd);\n\n        &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n        private static extern bool ImmSetOpenStatus(IntPtr hIMC, bool bOpen);\n\n        &#91;DllImport(\"imm32.dll\", CharSet = CharSet.Auto)]\n        private static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);\n\n        &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n        private static extern IntPtr GetKeyboardLayout(uint idThread);\n\n        &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n        private static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);\n\n        &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n        private static extern bool UnloadKeyboardLayout(IntPtr hkl);\n\n        &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n        private static extern IntPtr GetForegroundWindow();\n\n        &#91;DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);\n\n        \/\/ \u5b9a\u4e49\u952e\u76d8\u5e03\u5c40\u5e38\u91cf\n        private const uint KLF_ACTIVATE = 0x00000001;\n        private const string ENGLISH_KEYBOARD_LAYOUT = \"00000409\"; \/\/ \u82f1\u8bed\uff08\u7f8e\u56fd\uff09\u7684\u952e\u76d8\u5e03\u5c40 ID\n\n        \/\/----------\u6a21\u62df\u6309\u952e\n        \/\/ \u5bfc\u5165 Windows API,\u6a21\u62df\u6309\u952e\n        &#91;DllImport(\"user32.dll\")]\n        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);\n\n        \/\/ \u5b9a\u4e49\u865a\u62df\u952e\u7801\n        private const int VK_LWIN = 0x5B; \/\/ WIN \u952e\n        private const int VK_R = 0x52;    \/\/ R \u952e\n        private const int KEYEVENTF_KEYUP = 0x02; \/\/ \u6309\u952e\u91ca\u653e\n        public PDCForm()\n        {\n            InitializeComponent();\n        }\n\n        private void PDCForm_Load(object sender, EventArgs e)\n        {\n\n        }\n\/\/=========================================================================================================\n        \/\/nltest \/trusted_domains\n        private void trusted_domains_Click(object sender, EventArgs e)\n        {\n\n        }\n\/\/=========================================================================================================\n\n        private void BtnHosts_Click(object sender, EventArgs e)\n        {\n            \/\/ \u83b7\u53d6\u5f53\u524d\u524d\u666f\u7a97\u53e3\u7684\u53e5\u67c4\n            IntPtr hWnd = GetForegroundWindow();\n\n            \/\/ \u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684\u952e\u76d8\u5e03\u5c40\n            uint threadId = GetWindowThreadProcessId(hWnd, IntPtr.Zero);\n            IntPtr hKL = GetKeyboardLayout(threadId);\n\n            \/\/ \u68c0\u67e5\u5f53\u524d\u952e\u76d8\u5e03\u5c40\u662f\u5426\u662f IME\n            if (ImmIsIME(hKL))\n            {\n                Console.WriteLine(\"\u5f53\u524d\u8f93\u5165\u6cd5\u662f IME\uff0c\u6b63\u5728\u5207\u6362\u5230\u82f1\u8bed\u8f93\u5165\u6cd5...\");\n\n                \/\/ \u52a0\u8f7d\u82f1\u8bed\u952e\u76d8\u5e03\u5c40\n                IntPtr englishLayout = LoadKeyboardLayout(ENGLISH_KEYBOARD_LAYOUT, KLF_ACTIVATE);\n\n                if (englishLayout != IntPtr.Zero)\n                {\n                    Console.WriteLine(\"\u5df2\u5207\u6362\u5230\u82f1\u8bed\u8f93\u5165\u6cd5\u3002\");\n                }\n                else\n                {\n                    Console.WriteLine(\"\u65e0\u6cd5\u52a0\u8f7d\u82f1\u8bed\u952e\u76d8\u5e03\u5c40\u3002\");\n                }\n            }\n            else\n            {\n                Console.WriteLine(\"\u5f53\u524d\u8f93\u5165\u6cd5\u5df2\u7ecf\u662f\u975e IME \u8f93\u5165\u6cd5\uff08\u5982\u82f1\u8bed\uff09\u3002\");\n            }\n\n        \/\/-----------------------\n\n        \/\/ \u6ce8\u518c\u8868\u8def\u5f84\n        string registryPath = @\"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\";\n\n            \/\/ \u8bbe\u7f6eUAC\u7b49\u7ea7\n            int uacLevel = 0; \/\/ 1 = \u9ed8\u8ba4\u7b49\u7ea7\uff0c2 = \u6700\u9ad8\u7b49\u7ea7\uff0c0 = \u5173\u95edUAC\n            SetUACLevel(registryPath, uacLevel);\n\n            Console.WriteLine(\"UAC\u7b49\u7ea7\u5df2\u8c03\u6574\uff0c\u8bf7\u91cd\u542f\u8ba1\u7b97\u673a\u4f7f\u66f4\u6539\u751f\u6548\u3002\");\n\n\n            \/\/ \u7981\u7528\u8f93\u5165\u8bbe\u5907 3000 \u6beb\u79d2\uff08\u5373 3 \u79d2\uff09\n            \/\/InputDisabler.TemporarilyDisableInput(10000);\n\n            \/\/ hosts \u6587\u4ef6\u8def\u5f84\n            string hostsFilePath = Path.Combine(Environment.SystemDirectory, \"drivers\", \"etc\", \"hosts\");\n\n            \/\/ \u5907\u4efd\u6587\u4ef6\u8def\u5f84\n            string backupFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), \"hosts_backup.txt\");\n\n            try\n            {\n                \/\/ \u68c0\u67e5 hosts \u6587\u4ef6\u662f\u5426\u5b58\u5728\n                if (File.Exists(hostsFilePath))\n                {\n                    \/\/ \u590d\u5236 hosts \u6587\u4ef6\u5230\u5907\u4efd\u8def\u5f84\n                    File.Copy(hostsFilePath, backupFilePath, overwrite: true);\n                    Console.WriteLine($\"hosts \u6587\u4ef6\u5df2\u5907\u4efd\u5230: {backupFilePath}\");\n                    \/\/ \u6a21\u62df\u6309\u4e0b WIN + R\n                    keybd_event(VK_LWIN, 0, 0, UIntPtr.Zero); \/\/ \u6309\u4e0b WIN \u952e\n                    keybd_event(VK_R, 0, 0, UIntPtr.Zero);    \/\/ \u6309\u4e0b R \u952e\n                    keybd_event(VK_R, 0, KEYEVENTF_KEYUP, UIntPtr.Zero); \/\/ \u91ca\u653e R \u952e\n                    keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, UIntPtr.Zero); \/\/ \u91ca\u653e WIN \u952e\n\n                    \/\/ \u7b49\u5f85 Run \u5bf9\u8bdd\u6846\u6253\u5f00\n                    System.Threading.Thread.Sleep(500);\n                    \/\/ \u6a21\u62df\u8f93\u5165 \"cmd\"\n                    SendKeys.SendWait(\"cmd\");\n                    \/\/ \u6a21\u62df\u8f93\u5165 \"cmd\" \u5e76\u6309\u4e0b Enter\n                    System.Windows.Forms.SendKeys.SendWait(\"^(+{ENTER})\");\n                    \/\/ \u7b49\u5f85 CMD \u7a97\u53e3\u6253\u5f00\n                    System.Threading.Thread.Sleep(1000);\n\n                    \/\/ \u6a21\u62df\u8f93\u5165\u547d\u4ee4\u5e76\u6309\u4e0b\u56de\u8f66\u952e\n\n                    SendKeys.SendWait(\"echo. > C:\\\\Windows\\\\System32\\\\drivers\\\\etc\\\\hosts{ENTER}\");\n                    SendKeys.SendWait(\"echo 10.21.129.2 pdc.chana.com >> C:\\\\Windows\\\\System32\\\\drivers\\\\etc\\\\hosts{ENTER}\");\n                    SendKeys.SendWait(\"ipconfig \/flushdns{ENTER}\");\n                    System.Threading.Thread.Sleep(2000);\n                    SendKeys.SendWait(\"exit{ENTER}\");\n                }\n                else\n                {\n                    Console.WriteLine(\"\u672a\u627e\u5230 hosts \u6587\u4ef6\u3002\");\n                }\n            }\n            catch (UnauthorizedAccessException)\n            {\n                Console.WriteLine(\"\u8bf7\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u6b64\u7a0b\u5e8f\u3002\");\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine($\"\u5907\u4efd hosts \u6587\u4ef6\u65f6\u53d1\u751f\u9519\u8bef: {ex.Message}\");\n            }\n        }\n        \/\/=========================================================================================================\n        static void SetUACLevel(string registryPath, int uacLevel)\n        {\n            try\n            {\n                \/\/ \u4fee\u6539\u6ce8\u518c\u8868\u4e2d\u7684\u952e\u503c\n                Registry.SetValue(registryPath, \"ConsentPromptBehaviorAdmin\", uacLevel, RegistryValueKind.DWord);\n                Registry.SetValue(registryPath, \"EnableLUA\", 1, RegistryValueKind.DWord); \/\/ \u542f\u7528UAC\n                Console.WriteLine(\"UAC\u7b49\u7ea7\u5df2\u8bbe\u7f6e\u4e3a: \" + uacLevel);\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine(\"\u4fee\u6539\u6ce8\u518c\u8868\u65f6\u51fa\u9519: \" + ex.Message);\n            }\n            static void SetUACLevel(string registryPath, int uacLevel)\n            {\n                try\n                {\n                    \/\/ \u4fee\u6539\u6ce8\u518c\u8868\u4e2d\u7684\u952e\u503c\n                    Registry.SetValue(registryPath, \"ConsentPromptBehaviorAdmin\", uacLevel, RegistryValueKind.DWord);\n                    Registry.SetValue(registryPath, \"EnableLUA\", 1, RegistryValueKind.DWord); \/\/ \u542f\u7528UAC\n                    Console.WriteLine(\"UAC\u7b49\u7ea7\u5df2\u8bbe\u7f6e\u4e3a: \" + uacLevel);\n                }\n                catch (Exception ex)\n                {\n                    Console.WriteLine(\"\u4fee\u6539\u6ce8\u518c\u8868\u65f6\u51fa\u9519: \" + ex.Message);\n                }\n            }\n        }\n\/\/=========================================================================================================\n\n       \n\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u516c\u53f8\u7684\u5361\u5df4\u65af\u57fa\u5b89\u5168\u8f6f\u4ef6\u5bf9Hosts\u6587\u4ef6\u9501\u5b9a\u4e86\u3002\u4f7f\u7528\u4ee3\u7801\u521b\u5efa\u7684CMD\u8fdb\u7a0b\u4ea6\u6216\u662f\u76f4\u63a5\u64cd\u4f5cHosts\u6587\u4ef6\u90fd\u65e0\u6cd5\u64cd\u4f5c\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,25,4,5],"tags":[28,27],"class_list":["post-441","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-4","category-5","tag-net","tag-c"],"_links":{"self":[{"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/posts\/441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yuthk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=441"}],"version-history":[{"count":3,"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/yuthk.com\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions\/446"}],"wp:attachment":[{"href":"https:\/\/yuthk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yuthk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yuthk.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}