当我要更新值时,拒绝访问注册表项
我想编辑名为“usbstor”值的注册表项,这是我在更新方法中的代码
try { string path = baseRegistryKey + "\\" + SubKey; Registry.SetValue(path, KeyName, KeyValue, RegistryValueKind.DWord); return true; } catch (Exception e) { // AAAAAAAAAAARGH, an error! ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper()); return false; }
和path =“HKEY_LOCAL_MACHINE \ system \ currentControlset \ services \ usbstor”keyname =“start”当我运行代码时,我将获得“访问注册表项’HKEY_LOCAL_MACHINE \ system \ currentControlset \ services \ usbstor’被拒绝”是什么万阿英,蒋达清?
可执行文件
HKEY_LOCAL_MACHINE
始终是注册表中的受保护空间,因此您需要为至少Power User
提供权限,或者以管理员身份运行您的可执行文件(从您的解决方案构建的应用程序,应该在./bin
文件夹中)或禁用UAC
。 无论哪种方式,只要您没有配置/设置任何方式,它在Visual Studio中将会很麻烦。
请注意,如果您尝试使用Run.. -> regedit
,UAC也会提示您,这样不仅限制了您的应用,还限制了注册表本身的访问权限。
在Visual Studio中
在打开以Run as administrator
身份Run as administrator
之前提升Visual Studio足以从代码编辑注册表。
申请清单
为了将来的使用,您可能需要创建app.manifest
并将应用程序设置为始终需要管理员权限。 在Solution Explorer
右键单击您的项目,然后: Add -> New Item... -> Application Manifest File
。 在新创建的应用程序清单中,更改行:
到线
从现在开始,如果不以管理员身份运行,它将始终提示UAC。 如果以非管理员身份运行Visual Studio
,它将尝试以管理员身份重新启动IDE,并在继续之前提示执行此操作。