這個問題的總結是我有一個批量運行的命令,Some.exe args但它沒有輸出到控制臺。但是,如果像Some.exe args > test.txt. 我已經嘗試過類似的東西@Some.exe args并Some.exe args > CON讓它輸出到控制臺,但似乎都不起作用。
有沒有其他可行的方法?
這是我之前問的一個問題DOORS make console the interactive window 的后續問題。
我正在通過批處理腳本呼叫一個名為 DOORS 的程式。它運行一個簡單的腳本,hello.dxl看起來像
cout << "Hello world"
批處理腳本,Run.bat看起來像
"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl
運行時,螢屏上不會出現任何輸出,也沒有彈出視窗或任何東西(如果hello.dxl說print("Hello World")會彈出互動式視窗,但不會彈出cout)
如果我添加> test.txt到命令的末尾
"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > test.txt
它成功輸出Hello World到test.txt。我注意到的是,使用print("Hello World")時沒有發送到test.txt檔案的輸出,并且彈出了一個互動式視窗,所以看起來cout是要走的路。
所以我雖然輸出可能不會在任何地方輸出,所以我嘗試添加> CON而不是嘗試強制它進入控制臺。
"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > CON
但這仍然導致空白輸出。
我還嘗試@在命令之前添加一個 , 如此Batch - 將程式輸出重定向到當前控制臺中所建議的那樣,例如
@"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl
或者
@"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > CON
但那里也沒有運氣
我會嘗試在沒有 DOORS 的情況下重現此問題,但我首先不知道是什么原因造成的。
編輯:我真的不想使用> test.txt & type test.txt,因為這是我正在使用的當前解決方法。但理想情況下,我不希望它輸出到文本檔案
uj5u.com熱心網友回復:
基于@PA.的建議,該程式以某種方式阻止輸出到標準輸出,但如果它被重定向則不會,所以我撰寫了一個小的 C# 控制臺應用程式,它看起來像
using System;
using System.Diagnostics;
using Process process = new();
process.EnableRaisingEvents = true;
process.ErrorDataReceived = ErrOut;
process.OutputDataReceived = StdOut;
process.StartInfo.FileName = args[0].Trim();
process.StartInfo.Arguments = args[1].Trim();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
static void ErrOut(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
if (dataReceivedEventArgs.Data != null)
{
Console.WriteLine(dataReceivedEventArgs.Data);
}
}
static void StdOut(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
if (dataReceivedEventArgs.Data != null)
{
Console.WriteLine(dataReceivedEventArgs.Data);
}
}
然后我使用命令將其匯出到單個 exe
dotnet publish /p:DebugType=None /p:DebugSymbols=false /p:PublishReadyToRun=true /p:PublishSingleFile=true /p:PublishReadyToRunShowWarnings=true /p:PublishTrimmed=false /p:IncludeNativeLibrariesForSelfExtract=true /p:IncludeAllContentForSelfExtract=true "RedirectOutput.csproj" -o "." -c release
我添加的然后只是將帶有該 exe 的檔案夾添加到我的路徑環境變數中。
現在我可以做
RedirectOutput "C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" "-u test -pass testPass -b hello.dxl"
我得到了想要的輸出
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/478119.html
