2014-04-25 28 views
0

我正嘗試建立在C#中的小控制檯應用程序。我想運行該程序並將TFS中的所有待處理更改保存爲.txt文件。但我無法得到參數工作。有人能幫我嗎?運行「tf.exe狀態」在C#中,並保存結果

這裏是我的代碼,到目前爲止,我haved完成:

string argument = "@tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt"; 

try 
{ 
    Process process = new Process(); 
    process.StartInfo.Arguments = "@call" + " " + "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\Tools\\VsDevCmd.bat"; 
    process.StartInfo.FileName = "cmd.exe"; 
    process.StartInfo.Verb = "runas"; 
    process.StartInfo.Arguments = argument; 
    process.StartInfo.CreateNoWindow = false; 
    process.Start(); 
} 
catch (Exception ex) 
{ 
    Console.WriteLine(ex.ToString()); 
    Console.ReadKey(); 
} 

回答

1

A我真的不知道,我知道你想打電話,到底是什麼。 讓我們假設你想運行從C#應用程序下面的命令行,因爲如果你想從一個命令行調用它:

tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt" 

我會使用此代碼:

string arguments = @"/C tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt"; 
this.process = new Process(); 
this.process.StartInfo.FileName = @"cmd.exe"; 
this.process.StartInfo.Arguments = arguments; 

this.process.Start(); 

編輯: 如果這就是您所有的控制檯應用程序所做的,爲什麼不考慮創建批處理(.BAT/.CMD)文件而不是C#應用程序?

+0

也許會更好地工作,但我不知道如何創建使用命令行.bat文件。你能給我一些提示嗎? – robertdahlberg

+0

我認爲他的意思是不要使用C#程序,只需要一個批處理文件,其中包含調用'tf status ...'的頂級示例,即一行代碼包含您需要的所有內容。 –

0

我想你必須從進程讀取標準錯誤和輸出開始:

Process process = new Process(); 
process.StartInfo.Arguments = @"status PATH /recursive"; 
process.StartInfo.FileName = "tf.exe"; 
process.StartInfo.CreateNoWindow = false; 
process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardError = true; 
process.StartInfo.RedirectStandardOutput = true; 
process.Start(); 

var st = process.StandardOutput.ReadToEnd(); 
var err = process.StandardError.ReadToEnd(); 

但解析TF輸出是不容易的,我想建議使用TFS API作爲@Mare說

0

你並不需要創建在C#應用程序中的文本文件保存。只需在命令末尾使用參數「(...)> [文件名] .txt」即可。

的「>」符號發送任何命令的結果到一個文件。