为什么我可以在注册表编辑器中更改值,但不能在以管理员身份运行的 C# 表单应用程序中更改值?

问题描述 投票:0回答:1

我尝试在 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" />
也没有帮助。


相关主题和链接

  1. Win32 API 函数以编程方式启用/禁用设备
  2. https://www.tenforums.com/tutorials/80323-enable-disable-microphone-windows.html
c# windows security audio registry
1个回答
0
投票

注册表访问权限问题的答案通过这两个答案解决:

  1. https://stackoverflow.com/a/38727406/356726
  2. https://stackoverflow.com/a/17047190/356726

但是,以这种方式更改声音设备会导致设备挂起并需要重新启动。虽然从技术上来说这对我来说不是一个解决方案,但这是另一个话题。

© www.soinside.com 2019 - 2024. All rights reserved.