我想輸出openfiles進程的結果到一個文件,但我沒有得到任何結果有人可以解釋爲什麼?我已經嘗試了2種不同的方式。如果我使用命令提示符並運行相同的過程,它會將結果顯示到我的文件中。運行過程openfiles輸出結果到文件
更新:我添加了變化redirectstandardoutput第三種方法=真正的 我嘗試這樣做,現在我得到一個文件,但沒有結果
更新: 我發現這個問題是與構建選項被設置到x86時,在64位系統上這樣做,我認爲它運行的是32位版本的openfiles。我通過運行我的應用程序並使用RedirectStandardError流進行了測試,我首先應該這樣做:)這就是它所說的「錯誤:目標系統必須運行32位操作系統」。
//First Method
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "openfiles";
proc.StartInfo.Arguments = "/query /FO CSV /v > " + "\"" + Application.StartupPath + @"\OpenFiles.log" + "\"";
proc.Start();
}
//Second method
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "cmd";
proc.StartInfo.Arguments = "/C openfiles /query /FO CSV /v > " + "\"" + Application.StartupPath + @"\OpenFiles.log" + "\"";
proc.Start();
}
//Third method
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = "openfiles";
proc.StartInfo.Arguments = "/query /FO CSV /v";
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
if (output != null)
File.WriteAllText(Path.Combine(Application.StartupPath, "OpenFiles.log"), output);
}
[輸出重定向到文本文件C#(的可能重複http://stackoverflow.com/questions/16256587/redirecting-output-to-the-text-file-c-sharp) – 2014-09-24 17:29:46