c# - 设置注册表值抛出 UnauthorizedAccessException

标签 c#

<分区>

我有一个 C# 应用程序,我正在尝试通过注册表编辑服务。我正在使用需要管理员权限才能运行我的应用程序的 list 文件。尽管如此,这段代码还是会抛出

System.UnauthorizedAccessException: Cannot write to the registry key.

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

有人知道如何解决这个问题吗?

最佳答案

按照下面的代码,注意附加的true参数:

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

关于c# - 设置注册表值抛出 UnauthorizedAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10339990/

相关文章:

c# - 加入一个线程无一异常(exception)地停止执行

c# - 如何迭代 System.Windows.SystemParameters?

C# PowerShell 在桌面上创建空白文本文件

c# - 在等待新线程产生时调用 thread.sleep()

c# - Assembly.GetExecutingAssembly() 和 typeof(program).Assembly 之间的区别

c# - WPF 将 ObservableCollection 与转换器绑定(bind)

c# - 如何避免在C#、ASP.NET中更改单元格背景后删除Excel工作表的网格线颜色(默认浅灰色)?

c# - if 语句, bool 值

c# - (Outlook 2010) 在 C# 中获取邮件 header

c# - 如何拆分字符串然后重新加入它?