简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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