56581

How to export a registry in c#

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

Answer1:

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 + "\""; 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) { proc.Dispose(); } }

Answer2:

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 } }

Recommend

  • nonsense argument exception calling method
  • VBA - creating .vbs and .bat to issue keystrokes to external program
  • jenkins-maven-android when running throwing the error “android-sdk-linux/platforms” is not a directo
  • How to run Native Messaging example on Microsoft Edge?
  • How to find a COM dll installation working back from the Interop dll
  • How do you find the package a Delphi component comes from?
  • Access Windows Performance Counters in a locale independent way
  • Deploying pre-encrypted configuration files to a production environment
  • Instanciate service on startup in Angular2
  • Visual studio alerts workspace already exists
  • get passwords from chrome
  • Find VMID for running instance
  • How dotnet build chooses the output name
  • include dlls in visual studio c++ 2008
  • NSIS decompiler
  • netsh acl setting (need alternative method - registry settings?)
  • hide missing dates from x-axis ggplot2
  • aapt.exe'' finished with non-zero exit value 1
  • Tamper-proof configuration files in .NET?
  • Code in Job's Script Block after Start-Process Does not Execute
  • Why value captured by reference in lambda is broken? [duplicate]
  • Illegal mix of collations for operation for date/time comparison
  • MySQL WHERE-condition in procedure ignored
  • Running a C# exe file
  • sending/ receiving email in Java
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • Why joiner is not used after Sequence generator or Update statergy
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Recursive/Hierarchical Query Using Postgres
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • How can I use threading to 'tick' a timer to be accessed by other threads?