我正在編寫一個應用程序,它可以作爲標準WinForms應用程序啓動,也可以從命令行以無人蔘與模式啓動。該應用程序是使用VS 2k5標準WinForms模板構建的。如果從命令行啓動,則輸出到命令行
當從命令行執行應用程序時,我希望它輸出可由執行應用程序的腳本捕獲的信息。當我通過Console.WriteLine()直接執行此操作時,雖然可以通過管道將文件捕獲,但不會顯示輸出。
另一方面,我可以強制應用程序通過在kernel32上對AllocConsole()執行P/Invoke來彈出第二個控制檯。不過,這不是我想要的。我希望輸出出現在應用程序被調用的同一個窗口中。
這是突出的代碼,讓我彈出命令行控制檯:
<STAThread()> Public Shared Sub Main()
If My.Application.CommandLineArgs.Count = 0 Then
Dim frm As New ISECMMParamUtilForm()
frm.ShowDialog()
Else
Try
ConsoleControl.AllocConsole()
Dim exMan As New UnattendedExecutionManager(ConvertArgs())
IsInConsoleMode = True
OutputMessage("Application started.")
If Not exMan.SetSettings() Then
OutputMessage("Execution failed.")
End If
Catch ex As Exception
Console.WriteLine(ex.ToString())
Finally
ConsoleControl.FreeConsole()
End Try
End If
End Sub
Public Shared Sub OutputMessage(ByVal msg As String, Optional ByVal isError As Boolean = False)
Trace.WriteLine(msg)
If IsInConsoleMode Then
Console.WriteLine(msg)
End If
If isError Then
EventLog.WriteEntry("ISE CMM Param Util", msg, EventLogEntryType.Error)
Else
EventLog.WriteEntry("ISE CMM Param Util", msg, EventLogEntryType.Information)
End If
End Sub