none
How to export/import registry key programmatically?

    Question

  • Does anyone know how I can export/import registry keys/subkeys programmatically in C# and .NET?  I cannot find a way using Microsoft.Win32 namespace.

     

    Thanks!

    Friday, May 09, 2008 2:35 PM

Answers

  • You can use RegSaveKey and RegRestoreKey. They are not part of .Net BCL and you must pinvoke Windows API directly. However, you need SeBackupPrivilege and SeRestorePrivilege for these APIs. Normally, only members of Administrators and Backup Operators group have the privilege.

    You can also use RegistryKey.GetSubKeyNames to travel the hierarchy of the registry and save each subkey individually in your own format.see Import/Export registry sections as XML for one example.

    Friday, May 09, 2008 4:02 PM

All replies

  • You can use RegSaveKey and RegRestoreKey. They are not part of .Net BCL and you must pinvoke Windows API directly. However, you need SeBackupPrivilege and SeRestorePrivilege for these APIs. Normally, only members of Administrators and Backup Operators group have the privilege.

    You can also use RegistryKey.GetSubKeyNames to travel the hierarchy of the registry and save each subkey individually in your own format.see Import/Export registry sections as XML for one example.

    Friday, May 09, 2008 4:02 PM
  • That code is horrible.
    Thursday, November 26, 2009 11:00 AM
  • public void ExportKey(string RegKey, string SavePath)
            {
                string path = "\"" + SavePath + "\"";
                string key = "\"" + RegKey + "\"";

                var proc = new Process();
                try
                {
                    proc.StartInfo.FileName = "regedit.exe";
                    proc.StartInfo.UseShellExecute = false;
                    proc = Process.Start("regedit.exe", "/e " + path + " " + key + "");

                    if (proc != null) proc.WaitForExit();
                }
                finally
                {
                    if (proc != null) proc.Dispose();
                }

    }

    • Proposed as answer by SivaPrasanth Wednesday, August 10, 2011 6:06 AM
    Thursday, June 30, 2011 9:00 AM