mirror of
https://github.com/2dust/v2rayN.git
synced 2025-09-04 17:26:20 +00:00
54 lines
No EOL
1.4 KiB
C#
54 lines
No EOL
1.4 KiB
C#
using Microsoft.Win32;
|
|
|
|
namespace ServiceLib.Common
|
|
{
|
|
internal static class WindowsUtils
|
|
{
|
|
private static readonly string _tag = "WindowsUtils";
|
|
|
|
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)
|
|
{
|
|
Logging.SaveLog(_tag, ex);
|
|
}
|
|
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)
|
|
{
|
|
Logging.SaveLog(_tag, ex);
|
|
}
|
|
finally
|
|
{
|
|
regKey?.Close();
|
|
}
|
|
}
|
|
}
|
|
} |