我尝试在 C# 中启用声音设备。这导致
System.Security.SecurityException: 'Requested registry access is not allowed.'
。只需加载(读取)密钥即可,因此路径是正确的。
即使我以管理员身份运行 C# 程序,也会发生同样的情况。
string baseKeyName = device.RegistryPath;
RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey? regKey = localMachine.OpenSubKey(baseKeyName, true);
// System.Security.SecurityException: 'Requested registry access is not allowed.'
if (regKey == null) return false; // with false (read only) I get the regKey
regKey.SetValue("DeviceState", deviceState, RegistryValueKind.DWord);
regKey.Close();
在
Registry Editor
中做同样的事情就很好了。
知道为什么 C# 中没有授予权限吗?
更新1:
我尝试了
RegistryView.Registry64
和RegistryView.Registry32
(没有产生钥匙)但没有成功。即使程序以管理员身份运行,也根本无法更新(只读)。
更新2:
我没想到它会起作用,因为我已经尝试过“以管理员身份运行”。但是清单中的
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
也没有帮助。
相关主题和链接
注册表访问权限问题的答案通过这两个答案解决:
但是,以这种方式更改声音设备会导致设备挂起并需要重新启动。虽然从技术上来说这对我来说不是一个解决方案,但这是另一个话题。