Tom
Tom

Reputation: 15131

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: enter image description here

Upvotes: 0

Views: 693

Answers (2)

user8461894
user8461894

Reputation:

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.

Upvotes: 1

Tom
Tom

Reputation: 15131

After some digging this turned out to be a space in the registry key name, removing this now works.

Upvotes: 0

Related Questions