2014-09-24 51 views
2

我想輸出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); 
} 
+0

[輸出重定向到文本文件C#(的可能重複http://stackoverflow.com/questions/16256587/redirecting-output-to-the-text-file-c-sharp) – 2014-09-24 17:29:46

回答

0

重定向不會像那樣工作,因爲> filename不被視爲「參數」。

通常情況下,你將捕捉到的輸出在您的應用程序並將其寫入文件自己:

proc.StartInfo.RedirectStandardOutput = true; 
proc.Start(); 
string output = proc.StandardOutput.ReadToEnd(); 
proc.WaitForExit(); 

File.WriteAllText(Path.Combine(Application.StartupPath, "OpenFiles.log"), output); 
+1

我確實看到了有關重新定向標準輸出的其他問題/答案,但是當我嘗試這樣做時也不起作用。讓我再試一次,我將使用標準輸出找到的更新。謝謝所有 – 2014-09-24 17:38:25

+0

謝謝你的答案,但由於某種原因,這對我不起作用,我運行了另一個項目,它在codeproject上做着與我完全一樣的事情,如果我運行他的項目,他的代碼工作,所以我只是感到困惑。我沒有:( – 2014-09-24 20:47:46

+1

我發現這個問題是在64位系統上這樣做的時候將build選項設置爲x86我認爲它運行的是32位版本的openfiles我通過運行我的應用程序並使用RedirectStandardError流進行測試我應該一直在做:)這就是它說的「錯誤:目標系統必須運行32位操作系統。」 – 2014-09-24 21:20:00

相關問題