C#Set Registry Value抛出UnauthorizedAccessException

Nik*_*Nik 15 c#

我有一个C#应用程序,我正在尝试通过注册表编辑服务.我正在使用需要管理员权限才能运行我的应用程序的清单文件.尽管如此,这段代码抛出System.UnauthorizedAccessException: Cannot write to the registry key:

RegistryKey key = Registry.LocalMachine.OpenSubKey ("SYSTEM\\CurrentControlSet\\services\\Tomcat7");
key.SetValue ("Start", 2, RegistryValueKind.DWord);
Run Code Online (Sandbox Code Playgroud)

有没有人对如何解决这个问题有任何想法?

小智 24

Plz遵循以下代码,请注意其他true参数:

RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\Tomcat7",true);
key.SetValue("Start", 2, RegistryValueKind.DWord);
Run Code Online (Sandbox Code Playgroud)


Eri*_*Cal 6

这可能会有所帮助,

链接到堆栈溢出的类似问题

看起来您正在打开只读密钥…Google是您的朋友。