33

C# winforms - How can I import a reg file into the registry? The following code is displaying a confirmation box to the user (yes/no).

Process regeditProcess = Process.Start("key.reg", "/S /q"); // not working
regeditProcess.WaitForExit(); 
1
  • 8
    Why shouldn't he do this? Commented Jan 31, 2010 at 11:22

4 Answers 4

53

Send the file as a parameter to regedit.exe:

Process regeditProcess = Process.Start("regedit.exe", "/s key.reg");
regeditProcess.WaitForExit();
Sign up to request clarification or add additional context in comments.

2 Comments

One helpful hint, make sure the file name is enclosed with quotes if it contains spaces as it would on the command line.
In Win 11 the /s parameter causes a crash. Key content added OK if it's removed
15

The code in answer 2 is correct, but not complete. It will work when the directory where you are refering to has no spacings in the path/file you are refering to example C:\ProgramFiles\key.reg will work fine, but C:\Program Files\key.reg WON'T WORK because there are spaces in the path.

The solution:

string directory= @"C:\Program Files (x86)\key.reg";
Process regeditProcess = Process.Start("regedit.exe", "/s \"" + directory + "\"");
regeditProcess.WaitForExit();

Comments

8

I tried to invoke RegEdit, but each time I got a confirm prompt (UAC enabled, no elevated permissions). Instead of RegEdit I recommand "reg.exe" (which is included in Windows since XP)

            Process proc = new Process();

            try
            {
                proc.StartInfo.FileName = "reg.exe";
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.UseShellExecute = false;

                string command = "import " + path;
                proc.StartInfo.Arguments = command;
                proc.Start();

                proc.WaitForExit();
            }
            catch (System.Exception)
            {
                proc.Dispose();
            }

No dialog, no prompt.

The command is something like "reg import path/to/the/reg.reg"

1 Comment

Warning: There will be a malware called HEUR/QVM03.0.0000.Malware.Gen once you scanned your app with this method in Virustotal.
6

Instead of executing a .reg file, you should be able to make your changes to the registry using the functionality provided in the Microsoft.Win32 namespace.

It is quite easy to create a registry key using this API:

RegistryKey key = Registry.CurrentUser.CreateSubKey("Names");
key.SetValue("Name", "Isabella");
key.Close();

Unless you need to create a bulk load of new keys, I believe the API is a more scalable and maintable approach. If at some point, you need decide to make it optional to write your settings in the system-wide or the per-user branch of the registry, most of your code will be reusable for both cases. Simply pick another key to do the changes upon.

Maybe more important, the API lets you specify exactly (in code) how to handle cases where the key(s) you are inserting already exists in the registry. Should i delete the existing keys and insert mine, updates values within the existing keys, silently ignore or raise an exception?

4 Comments

Is there any particular reason he should be doing it using the API instead?
Assume the OP needs to install an official patch, which is a huge reg file. There are always valid reasons to do things.
I can tell is a huge reg file. thats way setValue for each can be pain in the *ss.
I agree with the three of you, that a reg is the right choice in some cases, and I have updated my answer with some arguments for why/when to use the API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.