简体   繁体   English

以管理员身份导入注册表项

[英]import registry key as administrator

In my program I am trying to import a registry key upon an event however, I am receiving the error "Cannot import key: Error opening the file. There may be a disk or file system error.". 在我的程序中,我尝试在发生事件时导入注册表项,但是,我收到错误消息“无法导入密钥:打开文件时出错。可能存在磁盘或文件系统错误。”。

Looking at this I think it is due to me not running the process as an admin. 看着这个,我认为这是由于我没有以管理员身份运行该过程。 My code is as follows: 我的代码如下:

   private void ImportRegKey_Click(object sender, EventArgs e)
   {
       if (System.Environment.OSVersion.Version.Major >=6)
        {
             Process regeditProcess = Process.Start("regedit.exe", "Key.reg");
                 regeditProcess.StartInfo.Verb = "runas";
        }
   }

Any advise would be appreciated, thanks. 任何建议,将不胜感激,谢谢。

Edit 编辑

The Key is part of the content of my project: 密钥是我的项目内容的一部分: 在此处输入图片说明

The code you have above should work: 您上面的代码应该可以工作:

 Process regeditProcess = Process.Start("regedit.exe", "Key.reg");
                 regeditProcess.StartInfo.Verb = "runas";

Remember though that spaces can cause issues. 请记住,尽管空格会引起问题。

经过一番挖掘后,发现这是注册表项名称中的空格,现在可以删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM