如何在c#中导出注册表

时间:2013-05-01 10:56:10

标签: c# command-line registry export

我一直在尝试将注册表文件导出并保存到任意位置,代码正在运行。但是,在指定路径和保存时,该功能不起作用,也不会导出注册表。也没有显示错误。

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

2 个答案:

答案 0 :(得分:5)

您需要在/e参数后添加一个空格,以便您的代码为:

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

答案 1 :(得分:5)

regedit.exe需要提升权限。 reg.exe是更好的选择。它不需要任何提升。

这是我们的工作。

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