2025-01-30 02:46:04 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.Win32;
|
2024-11-01 06:54:23 +00:00
|
|
|
|
|
|
|
|
namespace ServiceLib.Common
|
|
|
|
|
{
|
|
|
|
|
internal static class WindowsUtils
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
private static readonly string _tag = "WindowsUtils";
|
2025-01-05 06:50:31 +00:00
|
|
|
|
2024-11-01 06:54:23 +00:00
|
|
|
public static string? RegReadValue(string path, string name, string def)
|
|
|
|
|
{
|
|
|
|
|
RegistryKey? regKey = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
regKey = Registry.CurrentUser.OpenSubKey(path, false);
|
|
|
|
|
var value = regKey?.GetValue(name) as string;
|
|
|
|
|
return Utils.IsNullOrEmpty(value) ? def : value;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
Logging.SaveLog(_tag, ex);
|
2024-11-01 06:54:23 +00:00
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
regKey?.Close();
|
|
|
|
|
}
|
|
|
|
|
return def;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RegWriteValue(string path, string name, object value)
|
|
|
|
|
{
|
|
|
|
|
RegistryKey? regKey = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
regKey = Registry.CurrentUser.CreateSubKey(path);
|
|
|
|
|
if (Utils.IsNullOrEmpty(value.ToString()))
|
|
|
|
|
{
|
|
|
|
|
regKey?.DeleteValue(name, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
regKey?.SetValue(name, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
Logging.SaveLog(_tag, ex);
|
2024-11-01 06:54:23 +00:00
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
regKey?.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-30 02:46:04 +00:00
|
|
|
|
|
|
|
|
public static async Task RemoveTunDevice()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var sum = MD5.HashData(Encoding.UTF8.GetBytes("wintunsingbox_tun"));
|
|
|
|
|
var guid = new Guid(sum);
|
|
|
|
|
var pnpUtilPath = @"C:\Windows\System32\pnputil.exe";
|
|
|
|
|
var arg = $$""" /remove-device "SWD\Wintun\{{{guid}}}" """;
|
|
|
|
|
|
|
|
|
|
// Try to remove the device
|
|
|
|
|
await Utils.GetCliWrapOutput(pnpUtilPath, arg);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logging.SaveLog(_tag, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|