从CMD启动时Winform日志记录

时间:2019-08-08 13:00:33

标签: c# winforms cmd

我有一个Winform应用程序,可以通过双击它并将其用作前台Windows应用程序来正常启动,也可以从命令行使用参数启动它

我需要允许将应用程序Console.WriteLine功能日志打印到命令行。

当前,当我从命令行启动应用程序时,它会立即执行,并且光标会移至新行,就好像可以接受新命令一样。有什么建议吗?

编辑

我尝试检查以下answer下的答案(它们都不对我有用)

1 个答案:

答案 0 :(得分:0)

在搜索并阅读了measure.ts=ts(measure.df[,3],start = c(2017,1),frequency = 12) measure.ts = subset(measure.ts, month = c(3:12) ) train <- head(measure.ts, 0.77 * length(measure.ts)) test <- tail(measure.ts, 0.23 * length(measure.ts)) fit <- arima(train, c(1,1,0), seasonal = list(order = c(1,1,0), period = 12), method = "ML" ) fcast <-forecast(fit,h=12) 的文档并进行了一些测试之后

我到达了以下解决方案

我使用了answercompose :: [(a -> Bool)] -> (a -> Bool) compose [] = (\y -> False) compose (x:xs) = (\y -> (x y) || ((compose xs) y)) 中的函数kernel32.dll来使其正常工作

首先,我将WinForm应用程序作为控制台应用程序运行项目属性> 应用程序> 输出类型 = 控制台应用程序< / strong>

这将允许该项目作为控制台应用程序运行,因此当正常启动该应用程序(双击exe或从Visual Studio运行)时,它将启动Windows应用程序并启动一个cmd窗口

要解决此问题,我必须使用FreeConsole中的函数kernel32.dll才能释放cmd窗口

我最终得到的工作脚本是

FreeConsole

编辑上面的代码段将从应用程序中删除控制台窗口,这将释放kernel32.dll的处理程序,即在调用static class Program { [DllImport("kernel32.dll", SetLastError = true)] static extern bool FreeConsole(); [STAThread] static void Main(string[] args) { if (args.Length == 0) { FreeConsole(); MessageBox.Show("Application running by double-clicking on exe"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FrmMain()); }else { Console.WriteLine("Application is running from cmd with arguments); } } } Console时函数将引发异常Console.WriteLine

为避免此问题,我们不需要完全释放控制台窗口,只需将其隐藏即可,如下所示,使用Console.Write中的The handle is invalid

ShowWindow类中

kernel32.dll

program函数中

//[DllImport("kernel32.dll", SetLastError = true)]
//private static extern bool FreeConsole();

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);