我正在编写一个应用程序,它可以作为标准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