如何在 C# 注册表 class 中使用 REG_OPTION_OPEN_LINK
How to use REG_OPTION_OPEN_LINK in C# Registry class
我想打开一个符号注册表项 link。
According to Microsoft我需要用REG_OPTION_OPEN_LINK
才能打开
我搜索了将其添加到 OpenSubKey
函数的选项,但没有找到选项。只有 5 个重载函数,但其中 none 个允许添加可选参数:
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name)
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name, bool writable)
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name, RegistryKeyPermissionCheck permissionCheck)
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name, RegistryRights rights)
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name, RegistryKeyPermissionCheck permissionCheck, RegistryRights rights)
我能想到的唯一方法是使用 p\invoke 但也许我错过了它并且 C# 中有一个选项 类.
您不能使用普通的 RegistryKey
函数执行此操作。签入 the source code 后,似乎 ulOptions
参数始终作为 0
.
传递
唯一的方法是自己调用RegOpenKeyEx
,然后将结果SafeRegistryHandle
传递给RegistryKey.FromHandle
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.ComponentModel;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)]
static extern int RegOpenKeyExW(SafeRegistryHandle hKey, String lpSubKey,
int ulOptions, int samDesired, out SafeRegistryHandle hkResult);
public static RegistryKey OpenSubKeySymLink(this RegistryKey key, string name, RegistryRights rights = RegistryRights.ReadKey, RegistryView view = 0)
{
const int REG_OPTION_OPEN_LINK = 0x0008;
var error = RegOpenKeyExW(key.Handle, name, REG_OPTION_OPEN_LINK, ((int)rights) | ((int)view), out var subKey);
if (error != 0)
{
subKey.Dispose();
throw new Win32Exception(error);
}
return RegistryKey.FromHandle(subKey); // RegistryKey will dispose subKey
}
它是一个扩展函数,因此您可以在现有的子键或主键之一上调用它,例如 Registry.CurrentUser
。不要忘记在返回的 RegistryKey
:
上加上 using
using (var key = Registry.CurrentUser.OpenSubKeySymLink(@"SOFTWARE\Microsoft\myKey", RegistryRights.ReadKey))
{
// do stuff with key
}
我想打开一个符号注册表项 link。
According to Microsoft我需要用REG_OPTION_OPEN_LINK
才能打开
我搜索了将其添加到 OpenSubKey
函数的选项,但没有找到选项。只有 5 个重载函数,但其中 none 个允许添加可选参数:
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name)
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name, bool writable)
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name, RegistryKeyPermissionCheck permissionCheck)
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name, RegistryRights rights)
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(string name, RegistryKeyPermissionCheck permissionCheck, RegistryRights rights)
我能想到的唯一方法是使用 p\invoke 但也许我错过了它并且 C# 中有一个选项 类.
您不能使用普通的 RegistryKey
函数执行此操作。签入 the source code 后,似乎 ulOptions
参数始终作为 0
.
唯一的方法是自己调用RegOpenKeyEx
,然后将结果SafeRegistryHandle
传递给RegistryKey.FromHandle
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.ComponentModel;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)]
static extern int RegOpenKeyExW(SafeRegistryHandle hKey, String lpSubKey,
int ulOptions, int samDesired, out SafeRegistryHandle hkResult);
public static RegistryKey OpenSubKeySymLink(this RegistryKey key, string name, RegistryRights rights = RegistryRights.ReadKey, RegistryView view = 0)
{
const int REG_OPTION_OPEN_LINK = 0x0008;
var error = RegOpenKeyExW(key.Handle, name, REG_OPTION_OPEN_LINK, ((int)rights) | ((int)view), out var subKey);
if (error != 0)
{
subKey.Dispose();
throw new Win32Exception(error);
}
return RegistryKey.FromHandle(subKey); // RegistryKey will dispose subKey
}
它是一个扩展函数,因此您可以在现有的子键或主键之一上调用它,例如 Registry.CurrentUser
。不要忘记在返回的 RegistryKey
:
using
using (var key = Registry.CurrentUser.OpenSubKeySymLink(@"SOFTWARE\Microsoft\myKey", RegistryRights.ReadKey))
{
// do stuff with key
}