新增代理设置功能,优化模式切换逻辑,更新控制台输出选项

This commit is contained in:
muqing 2025-02-12 15:37:39 +08:00
parent 37e5c0b998
commit 672b63987a
4 changed files with 115 additions and 2 deletions

16
.vscode/tasks.json vendored Normal file
View File

@ -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"
}
]
}

View File

@ -2,6 +2,8 @@
namespace Debug;
public class ModeSwitch
{
private object? class_;
public event Action<int>? Enter;
private readonly string[] str = ["退出"];
private int mode = 0;
@ -11,6 +13,16 @@ public class ModeSwitch
public ModeSwitch(string title, string[] a, Action<int> Enter) : this(title, a, Enter, string.Empty)
{
}
public ModeSwitch(object class_, string title, string[] a, Action<int> 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<int> Enter, string message)
{
this.Enter = Enter;
@ -62,8 +74,16 @@ public class ModeSwitch
mode = v1 - 1;
}
else if (key.Key == ConsoleKey.Escape)
{
if (class_ != null)
{ // 通过反射创建新的实例
Type? type = class_?.GetType(); // 获取类型
object? newInstance = type?.GetConstructor(Type.EmptyTypes)?.Invoke(null);
}
else
{
IsEnd();
}
return;
}
else

View File

@ -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");
}

74
ProxySettingsHelper.cs Normal file
View File

@ -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;
}
}
}