不允许请求的注册表访问权限。即使在清单中有签名的 exe 和 admin Privileged
Requested registry access is not allowed. even with signed exe and admin Privileged in manifest
我正在尝试访问 HKEY_LOCAL_MACHINE 注册表,
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
我还用测试证书签署了可执行文件
这是函数:
static string GetDeviceGuid()
{
const string AdapterKey = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}";
RegistryKey regAdapters = Registry.LocalMachine.OpenSubKey(AdapterKey, RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.ReadKey);
string[] keyNames = regAdapters.GetSubKeyNames();
string devGuid = "";
foreach (string x in keyNames)
{
RegistryKey regAdapter = regAdapters.OpenSubKey(x);
object id = regAdapter.GetValue("ComponentId");
if (id != null && id.ToString() == "tap0901") devGuid = regAdapter.GetValue("NetCfgInstanceId").ToString();
}
return devGuid;
}
这是结果:
at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
at TestTun.TunTap.GetDeviceGuid()
我应该怎么做才能读取没有安全异常的注册表
我发现了问题,但我不知道为什么无法访问该地址
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\Properties
Registry Error on Regedit
Visual Studio Debug
我用正则表达式修正了我的代码
static string GetDeviceGuid()
{
const string AdapterKey = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}";
RegistryKey regAdapters = Registry.LocalMachine.OpenSubKey(AdapterKey);
string[] keyNames = regAdapters.GetSubKeyNames();
foreach (string x in keyNames) {
Console.WriteLine(x);
}
string devGuid = "";
Regex rx = new Regex(@"\d{4}",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (string x in keyNames)
{
if (!rx.IsMatch(x)) {
continue;
}
RegistryKey regAdapter = regAdapters.OpenSubKey(x);
object id = regAdapter.GetValue("ComponentId");
if (id != null && id.ToString() == "tap0901") devGuid = regAdapter.GetValue("NetCfgInstanceId").ToString();
}
return devGuid;
}
我正在尝试访问 HKEY_LOCAL_MACHINE 注册表,
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
我还用测试证书签署了可执行文件
这是函数:
static string GetDeviceGuid()
{
const string AdapterKey = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}";
RegistryKey regAdapters = Registry.LocalMachine.OpenSubKey(AdapterKey, RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.ReadKey);
string[] keyNames = regAdapters.GetSubKeyNames();
string devGuid = "";
foreach (string x in keyNames)
{
RegistryKey regAdapter = regAdapters.OpenSubKey(x);
object id = regAdapter.GetValue("ComponentId");
if (id != null && id.ToString() == "tap0901") devGuid = regAdapter.GetValue("NetCfgInstanceId").ToString();
}
return devGuid;
}
这是结果:
at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
at TestTun.TunTap.GetDeviceGuid()
我应该怎么做才能读取没有安全异常的注册表
我发现了问题,但我不知道为什么无法访问该地址
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\Properties
Registry Error on Regedit Visual Studio Debug
我用正则表达式修正了我的代码
static string GetDeviceGuid()
{
const string AdapterKey = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}";
RegistryKey regAdapters = Registry.LocalMachine.OpenSubKey(AdapterKey);
string[] keyNames = regAdapters.GetSubKeyNames();
foreach (string x in keyNames) {
Console.WriteLine(x);
}
string devGuid = "";
Regex rx = new Regex(@"\d{4}",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (string x in keyNames)
{
if (!rx.IsMatch(x)) {
continue;
}
RegistryKey regAdapter = regAdapters.OpenSubKey(x);
object id = regAdapter.GetValue("ComponentId");
if (id != null && id.ToString() == "tap0901") devGuid = regAdapter.GetValue("NetCfgInstanceId").ToString();
}
return devGuid;
}