如何在c#中导出注册表

问题描述 投票:0回答: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();
    }
}
c# command-line registry export
2个回答
10
投票

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

这就是我们所做的。

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

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
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.