1

I have created a registry file in C# to configure a client email account. I then crated a process then started to execute the reg file, like so:

  var proc = new Process();
                var pi = new ProcessStartInfo();
                pi.UseShellExecute = true;
                pi.FileName = strRegPath;
                proc.StartInfo = pi;

                proc.Start();

This works fine, but however it requires the use to interact with the importing of the regustry file by clicking yes. Is there anyway I can run this in the background without interacting with the user?

nate
  • 1,418
  • 5
  • 34
  • 73
  • 1
    Why you use registry file (.reg?)? you can read/write from registry using .net methods.. – ilansch Sep 20 '13 at 20:03
  • @ilansch Can you be a little bit more descriptive please – nate Sep 20 '13 at 20:04
  • 1
    http://stackoverflow.com/questions/1908539/net-registry-reading-writing – ilansch Sep 20 '13 at 20:05
  • This is a bit pointless. Why don't you call MessageBox.Show() yourself? Only call Start() when you get DialogResult.Yes. Of course the user has no idea what's going to happen to his registry so don't be shy about being chatty about it. You'll always get No if you don't. – Hans Passant Sep 20 '13 at 23:03

1 Answers1

3

REGEDIT.EXE has a /s option which supresses the dialog box.

So you would change the pi.FileName to regedit.exe

and add pi.Arguments = "/s " + strRegPath

and you don't need pi.UseShellExecute now because you aren't using the shell tables that link file extensions to an executable to open them.

EDIT: In fact, you don't even need ProcessStartInfo, you could just do this

System.Diagnostics.Process.Start("regedit.exe", "/s \""+strRegPath+"\"");

EDIT: User Access Contol (UAC)

However, unless you are using some sort of 3rd party reg file, you would be much better changing the registry entries using the Microsoft.Win32.Registry class.

UAC pops up when you start regedit because it can alter values in HKEY_LOCAL_MACHINE which requires elevation. However, I would have thought all of the client email account settings are under HKEY_CURRENT_USER. Subkeys below HKEY_CURRENT_USER can be accessed using the Registry class without requiring elevation, and so would not create any UAC pop up windows, also allowing the script to be used by logins that are not administrators. This is probably the much superior route than trying to use .reg files.

Strings
  • 1,674
  • 10
  • 16
  • I tried adding add pi.Arguments = "/s " + strRegPath, and the dialog box still pops up asking me if I want to run the reg file – nate Sep 20 '13 at 20:32
  • Yes I changed the pi.FileName to reedit.exe. I also removed all the ProcessStartInfo and used the proc.Start("regedit.exe", "/s \""+strRegPath+"\""); it still didn't work. I am not sure if it matters or not but I am using Windows 8 – nate Sep 20 '13 at 20:46
  • I may have misunderstood... Clicking 'yes' to what question? Is the window title "User Account Control" or "Registry Editor" – Strings Sep 20 '13 at 20:49
  • The UAC pops up with the registry editor icon. "User Account Control" is the title of the window. – nate Sep 23 '13 at 13:22
  • 1
    The solution I gave is to get rid of the window titled "Registry Editor". To stop the "UAC" window you need to search for questions about it, there are plenty already on StackOverflow - http://stackoverflow.com/questions/5427673/how-to-run-a-program-automatically-as-admin-on-windows-startup. After you get rid of UAC, you will also need my solution to make regedit silent so the process doesn't stall without updating the registry. However, if all the registry settings you want to set are in HKEY_CURRENT_USER, by using the Registry class, the changes can be made without elevation. – Strings Sep 23 '13 at 16:08