diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..7f159e7 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,16 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "dotnet", + "task": "build D:\\Debug\\Debug.csproj", + "file": "D:\\Debug\\Debug.csproj", + "group": { + "kind": "test", + "isDefault": true + }, + "problemMatcher": [], + "label": "dotnet: build D:\\Debug\\Debug.csproj" + } + ] +} \ No newline at end of file diff --git a/ModeSwitch.cs b/ModeSwitch.cs index f7ae498..9c16ab8 100644 --- a/ModeSwitch.cs +++ b/ModeSwitch.cs @@ -2,6 +2,8 @@ namespace Debug; public class ModeSwitch { + + private object? class_; public event Action? Enter; private readonly string[] str = ["退出"]; private int mode = 0; @@ -11,6 +13,16 @@ public class ModeSwitch public ModeSwitch(string title, string[] a, Action Enter) : this(title, a, Enter, string.Empty) { } + + public ModeSwitch(object class_, string title, string[] a, Action Enter, string message) + { + this.class_ = class_; + this.Enter = Enter; + str = a; + this.title = title; + this.message = message; + Run(); + } public ModeSwitch(string title, string[] a, Action Enter, string message) { this.Enter = Enter; @@ -63,7 +75,15 @@ public class ModeSwitch } else if (key.Key == ConsoleKey.Escape) { - IsEnd(); + if (class_ != null) + { // 通过反射创建新的实例 + Type? type = class_?.GetType(); // 获取类型 + object? newInstance = type?.GetConstructor(Type.EmptyTypes)?.Invoke(null); + } + else + { + IsEnd(); + } return; } else diff --git a/Program.cs b/Program.cs index a44e1ac..d9eabd4 100644 --- a/Program.cs +++ b/Program.cs @@ -30,7 +30,7 @@ public class Program { Console.Clear(); // ResetChars(); - string[] str = ["抽卡", "搜索引擎", Music.title, "小龙快跑", "贪吃蛇", "铁锈助手(联机模块)[未开发]"]; + string[] str = ["抽卡", "搜索引擎", Music.title, "小龙快跑", "贪吃蛇", "铁锈助手(联机模块)[未开发]", "服务器代理"]; var modeSwitch = new ModeSwitch("请选择你要执行的操作", str, (a) => { switch (a) @@ -55,6 +55,9 @@ public class Program // _ = new Tanchishe.Main(); _ = new Program(); break; + case 6: + _ = new ProxySettingsHelper(this); + break; } }, "作者:MuQing\n使用语言:C#\n使用IDE:VS Code\n使用框架:.NET 8.0 \n测试运行平台:Windows11\n项目地址:https://git.coldmint.top/muqing/ModelingMusicForm.git"); } diff --git a/ProxySettingsHelper.cs b/ProxySettingsHelper.cs new file mode 100644 index 0000000..c009d86 --- /dev/null +++ b/ProxySettingsHelper.cs @@ -0,0 +1,74 @@ +using Microsoft.Win32; +using System.Runtime.InteropServices; +namespace Debug; +#pragma warning disable CA1416 // 验证平台兼容性 +#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。 +public class ProxySettingsHelper +{ + // 引入WinINet API函数 + [DllImport("wininet.dll", SetLastError = true)] + private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); + + private const int INTERNET_OPTION_SETTINGS_CHANGED = 39; + private const int INTERNET_OPTION_REFRESH = 37; + + public string? proxy = "127.0.0.1:7890"; // 代理服务器地址 + public bool enableProxy = false; // true 开启代理,false 关闭代理 + public ProxySettingsHelper(object class_) + { + enableProxy = IsProxyEnabled(); + _ = new ModeSwitch(class_, "Proxy设置", ["关闭代理", "打开代理"], (a) => + { + switch (a) + { + case 0: + SetProxy(false, proxy); + break; + case 1: + SetProxy(true, proxy); + break; + } + _ = new ProxySettingsHelper(this); + }, $"代理已{(enableProxy ? "开启" : "关闭")},地址: {proxy}"); + } + + + public void SetProxy(bool enable, string proxyAddress) + { + string proxyEnable = enable ? "1" : "0"; + string proxyServer = enable ? proxyAddress : ""; + + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true)) + { + if (key != null) + { + key.SetValue("ProxyEnable", proxyEnable, RegistryValueKind.DWord); + key.SetValue("ProxyServer", proxyServer, RegistryValueKind.String); + } + } + + // 使更改立即生效 + InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); + InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); + } + + public bool IsProxyEnabled() + { + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", false)) + { + if (key == null) return false; + + object proxyEnable = key.GetValue("ProxyEnable"); + object proxyServer = key.GetValue("ProxyServer"); + + bool enabled = proxyEnable != null && (int)proxyEnable == 1; + if (enabled && proxyServer != null && proxyServer.ToString() != null) + { + proxy = proxyServer?.ToString(); + } + + return enabled; + } + } + +}