2013-05-16 50 views
1

我正在開發一個應用程序以從我的postgreSQL數據庫中用C#進行備份。 我使用下面的代碼執行並從pg_dump.exe中取出。無法從pg_dump.exe命令行得到輸出

ProcessStartInfo startinfo = new ProcessStartInfo 
     { 
      FileName = "\"C:\\Program Files (x86)\\pgAdmin III\\1.16\\pg_dump.exe\"", 
      Arguments = "--host XXX.XXX.XXX.XXX --port 5432 --username \"USERNAME\" --no-password --format plain --verbose --file \"D:\\MYBACKUP.backup\" \"MYDBNAME\"", 
      UseShellExecute = false, 
      RedirectStandardOutput = true, 
      CreateNoWindow = true 
     }; 

     Process proc = new Process(); 
     proc.StartInfo = startinfo; 
     proc.Start(); 


     while (!proc.StandardOutput.EndOfStream) 
     { 
      var r = proc.StandardOutput.ReadLine(); 
     } 

     proc.WaitForExit(); 
     Console.WriteLine(proc.ExitCode); 
     Console.ReadLine(); 

但proc.StandardOutput.ReadLine總是返回null!

我試圖把pg_dump.exe輸出到文件,命令propt這樣的:

C:\Program Files (x86)\pgAdmin III\1.16>pg_dump.exe > d:\log.txt 

但同樣的log.txt是空的!

在此先感謝。

回答

2

更換RedirectStandardOutputRedirectStandardError

我的工作代碼爲:

ProcessStartInfo startinfo = new ProcessStartInfo 
{ 
    FileName = "Path to pg_dump.exe", 
    Arguments = "Arguments", 
    UseShellExecute = false, 
    RedirectStandardError = true, 
    CreateNoWindow = true 
}; 

using (Process process = Process.Start(startinfo)) 
{ 
    using (StreamReader reader = process.StandardError) 
    { 
     StreamWriter sw = new StreamWriter(@"C:\log.txt"); 
     sw.WriteLine(reader.ReadToEnd()); 
     sw.Close(); 
    } 
}