2013-05-01 126 views
2

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

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

你在捕捉异常,然后既不显示也不记录它,所以我希望不会显示错误。你至少可以把catch变成一个'Exception e'并在其中插入一个'Console.WriteLine(e.Message)'。或者如果调试,只需在'catch'块中放置一个断点并查看异常情况。 – 2013-05-01 11:04:33

+0

我为异常创建了一个对象,并试图在消息框中显示它(如果存在)。没有例外 – 2013-05-01 11:11:21

+0

请您发布注册表项名称的示例,例如,您是否使用“HKLM”或“HKEY_LOCAL_MACHINE”,您是否确实拥有足够的权限来访问注册表项 – 2013-05-01 11:15:07

回答

4

您需要/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(); 
    } 
} 
+0

您的代码与我给出的 – 2013-05-01 11:25:21

+0

相同,对不起,我再次修改它,只是在“/ e”参数之后添加一个空格 – 2013-05-01 11:26:08

+1

该代码仍然不起作用。注册表仍然没有被导出。 – 2013-05-01 11:33:51

2

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