【发布时间】:2008-12-03 22:16:22
【问题描述】:
我正在编写一个应用程序,它既可以作为标准 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
【问题讨论】:
标签: command-line .net-2.0