我正在使用TF.exe命令列執行一些簡單的 TFS 命令,例如獲取最新版本的檔案 ( tf.exe vc get)、檢出一些檔案 ( tf.exe vc checkout),然后在使用結束時檢入它們 ( tf.exe vc checkin)。
經過一些測驗后,我注意到當我通過 Windows 命令提示符 ( cmd.exe) 運行簽入命令時,會顯示提示視窗,要求在簽入命令完成之前進行確認:
C:\Program Files\Microsoft Visual Studio\2022\Community
\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer>
tf.exe vc checkin "C:\TfsTestProject\test.txt" /comment:"Checked via command-line."

對于我的專案來說,這是一件好事,顯示簽入確認的提示視窗。
但是,當我通過代碼啟動 TF 命令時,我發現了一個問題:我必須啟動TF.exe重定向標準輸出的行程,這樣我才能顯示命令執行時發生的情況,為此我必須設定process.StartInfo.UseShellExecute = False. 但是當我這樣做時,提示視窗不再顯示,好像我正在使用/noprompt引數(而且我沒有使用它)。
所以,當我使用下面的代碼時,仍然顯示提示視窗,如圖所示,但我無法收到標準輸出流反饋:
Dim p = New Process()
' I removed the full path for clarity, but that's:
' "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
p.StartInfo.FileName = "TF.exe"
p.StartInfo.Arguments = $"vc checkin ""{testFilePath}"" /comment:""Checked via code."""
p.StartInfo.UseShellExecute = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.WaitForExit()
但是,當我使用我需要的其他代碼時,不再顯示簽入確認提示視窗,即使我沒有使用/noprompt引數:
Dim p = New Process()
' I removed the full path for clarity, but that's:
' "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
p.StartInfo.FileName = "TF.exe"
p.StartInfo.Arguments = $"vc checkin ""{testFilePath}"" /comment:""Checked via code."""
p.StartInfo.UseShellExecute = False ' <- That's the problem, apparently.
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.CreateNoWindow = True
p.EnableRaisingEvents = True
AddHandler p.OutputDataReceived, Sub(s, e) Console.WriteLine(e.Data)
p.Start()
p.BeginOutputReadLine()
p.WaitForExit()
我找不到有關該行為的任何資訊,也許這只是一個錯誤?
uj5u.com熱心網友回復:
對我感到羞恥,我在發布問題之前做了一些研究但一無所獲,但是在發布之后我發現了這個:
c# - 如何在不停止其對話框提示的情況下捕獲 tf.exe stderr?- 堆疊溢位
這個問題很老,因為那個問題是 10 年前的,面臨著同樣的“錯誤”。顯然有一個未記錄的引數/prompt使提示視窗顯示,即使 TF 命令不是通過 shell 執行的。
所以這段代碼(帶有未記錄的/prompt引數)有效:
Dim p = New Process()
' I removed the full path for clarity, but that's:
' "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
p.StartInfo.FileName = "TF.exe"
' With undocumented /prompt parameter the prompt window is
' displayed even when the command is not executed through shell.
p.StartInfo.Arguments = $"vc checkin ""{testFilePath}"" /prompt /comment:""Checked via code."""
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.CreateNoWindow = True
p.EnableRaisingEvents = True
AddHandler p.OutputDataReceived, Sub(s, e) Console.WriteLine(e.Data)
p.Start()
p.BeginOutputReadLine()
p.WaitForExit()
在很久以前的那個問題和答案之后,我又測驗了一點,發現我錯了,問題不在于沒有從 shell 啟動命令,當流(輸出或錯誤)被重定向時,問題確實發生了. 剛才設定的時候UseShellExecute = False,沒有重定向,提示視窗還是會顯示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/536778.html
標籤:网络tfstf-cli
