2

I have been trying to export and save registry files to an arbitrary location, the code is running. However, on specifying the path and saving, the function does not work and no registry is exported. There is no error shown either.

private static void Export(string exportPath, string registryPath)
{ 
    string path = "\""+ exportPath + "\"";
    string key = "\""+ registryPath + "\"";
    // string arguments = "/e" + path + " " + key + "";
    Process proc = new Process();

    try
    {
        proc.StartInfo.FileName = "regedit.exe";
        proc.StartInfo.UseShellExecute = false;
        //proc.StartInfo.Arguments = string.Format("/e", path, key);

        proc = Process.Start("regedit.exe", "/e" + path + " "+ key + "");
        proc.WaitForExit();
    }
    catch (Exception)
    {
        proc.Dispose();
    }
}
Arran
  • 24,648
  • 6
  • 68
  • 78
Aman Mehrotra
  • 373
  • 4
  • 7
  • 15
  • i created an object for the exception and tried to display it in a message box if present. No exception is being shown – Aman Mehrotra May 01 '13 at 11:11
  • Would you please post a sample for the registry key name, for example do you use "HKLM" or "HKEY_LOCAL_MACHINE", also are you sure that you have the enough permissions to access the registry keys – Amer Sawan May 01 '13 at 11:15
  • "HKEY_CURRENT_USER\AppEvents\EventLabels\AceBackup" . I have admin rights – Aman Mehrotra May 01 '13 at 11:17

2 Answers2

8

You need to add a space after the /e parameters so your code will be :

private static void Export(string exportPath, string registryPath)
{ 
    string path = "\""+ exportPath + "\"";
    string key = "\""+ registryPath + "\"";
    using (Process proc = new Process())
    {
        try
        {
            proc.StartInfo.FileName = "regedit.exe";
            proc.StartInfo.UseShellExecute = false;
            proc = Process.Start("regedit.exe", "/e " + path + " "+ key);
            proc.WaitForExit();
        }
        catch (Exception)
        {
            // handle exceptions
        }
    }
}
Amer Sawan
  • 2,126
  • 1
  • 22
  • 40
  • sorry, I modified it again, just add a space after the "/e" parameter – Amer Sawan May 01 '13 at 11:26
  • 1
    The code still doesn't work. The registry is still not getting exported. – Aman Mehrotra May 01 '13 at 11:33
  • it worked perfectly for me, do you have the UAC enabled, if yes try to set the proc.StartInfo.Verb="runas"; – Amer Sawan May 01 '13 at 11:40
  • @AmerSawan so this method can be invoked if I specify the registry path as in the whole path, but what about the export path? Like what string format will it be in? – Scarl Sep 24 '14 at 11:15
  • 1
    @Sara It should be a file path that the program have write permission on it, for example: Export(@"C:\Users\Public\Desktop\export.reg", @"HKEY_CURRENT_USER\Software") – Amer Sawan Sep 24 '14 at 15:07
  • @AmerSawan okay, so then the keys will be saved where? I mean once the keys are exported you'd lets say want to save them in a text file right? – Scarl Sep 24 '14 at 15:52
  • @Sara Yes it will exported into a .reg file which is text file written in special format, you can have more information about its syntax from [here](http://support.microsoft.com/kb/310516) – Amer Sawan Sep 24 '14 at 18:24
  • @AmerSawan okay thanks for that, now I'm trying to implement the same thing using java but I'm having trouble with the process? – Scarl Sep 25 '14 at 04:07
  • 2
    setting startinfo about accomplishes nothing since that proc instance is thrown away when you call proc = Process.Start. Also, use reg.exe instead of regedit.exe. It does not require UAC. – tdemay Feb 25 '17 at 15:19
8

regedit.exe requires elevated privileges. reg.exe is better choice. It does not require any elevation.

Here's what we do.

    void exportRegistry(string strKey, string filepath)
    {
        try
        {
            using (Process proc = new Process())
            {
                proc.StartInfo.FileName = "reg.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.Arguments = "export \"" + strKey + "\" \"" + filepath + "\" /y";
                proc.Start();
                string stdout = proc.StandardOutput.ReadToEnd();
                string stderr = proc.StandardError.ReadToEnd();
                proc.WaitForExit();
            }
        }
        catch (Exception ex)
        {
            // handle exception
        }
    }
tdemay
  • 649
  • 8
  • 23