2

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

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

2 回答 2

8

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
        }
    }
于 2017-02-25T15:18:22.487 回答
7

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

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
        }
    }
}
于 2013-05-01T11:21:15.047 回答