C# - Import reg file to the registry without user confirmation box

Send the file as a parameter to regedit.exe:

Process regeditProcess = Process.Start("regedit.exe", "/s key.reg");
regeditProcess.WaitForExit();

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();

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"

Tags:

C#

Registry