C#app命令行输出写入输入行

时间:2018-01-18 06:55:59

标签: c# powershell output command-line-interface

这可能是一个简单的问题,我只是没有弄清楚如何正确地说出来。无论如何,请允许我描述我的问题。

所以我写了一个程序,帮助用户管理他们的程序安装。出于显而易见的原因,我提供了对此应用程序的GUI和命令行访问(GUI在WPF中)。

无论如何,我的问题是输出一堆文本到命令行。这是我的代码:

    void WriteHelp()
    {
        // Attach to console to write out to it
        if (AttachConsole(-1))
        {
            Console.WriteLine("InstallAssist helps maintain your Shine Calendar installation.");
            Console.WriteLine("Version " + VERSION_STRING + " (" + VERSION_ID + ")");
            Console.WriteLine("Written by Jayke R. Huempfner. Copyright 2018.");
            Console.WriteLine();
            Console.WriteLine("Can be accessed through GUI or by using the arguments below:");
            Console.WriteLine();
            Console.WriteLine("Argument            Action");
            Console.WriteLine("--------            ------");
            Console.WriteLine("-about (-a)         Displays version/system info");
            Console.WriteLine("-hardreset (-hr)    Perform a hard reset");
            Console.WriteLine("-help (-?)          Displays this help page.");
            Console.WriteLine("-info (-i)          Displays version/system info");
            Console.WriteLine("-quiet (-q)         Quiet mode, don't display GUI");
            Console.WriteLine("-reset (-e)         Display reset options GUI");
            Console.WriteLine("-skipconfirm (-sc)  Skip confirmation GUI screen");
            Console.WriteLine("-softreset (-s)     Perform a soft reset");
            Console.WriteLine("-uninstall (-u)     Uninstall, keeping user data");
            Console.WriteLine("-uninstallall (-ua) Uninstall, deleting user data");
            Console.WriteLine();
            Console.WriteLine("More information available online at:");
            Console.WriteLine("https://shinecalendar.com/help/advanced");
        }
    }

看起来很简单,不是吗?我认为结果会非常明显。但是,当我进入PowerShell并访问它时,这就是我得到的:

PS C:\Users\jacob\Documents\Visual Studio 2017\Projects\ShineCalendar\ShineWin\bin\Release> .\InstallAssist.exe -help
PS C:\Users\jacob\Documents\Visual Studio 2017\Projects\ShineCalendar\ShineWin\bin\Release> InstallAssist helps maintain your Shine Calendar installation.
Version 0.8.0 (800)
Written by Jayke R. Huempfner. Copyright 2018.

Can be accessed through GUI or by using the arguments below:

Argument            Action
--------            ------
-about (-a)         Displays version/system info
-hardreset (-hr)    Perform a hard reset
-help (-?)          Displays this help page.
-info (-i)          Displays version/system info
-quiet (-q)         Quiet mode, don't display GUI
-reset (-e)         Display reset options GUI
-skipconfirm (-sc)  Skip confirmation GUI screen
-softreset (-s)     Perform a soft reset
-uninstall (-u)     Uninstall, keeping user data
-uninstallall (-ua) Uninstall, deleting user data

More information available online at:
https://shinecalendar.com/help/advanced

格式化出来都没问题,但问题是顶行被写入下一个命令输入。当我开始输入更多命令和诸如此类的东西时,它最终会覆盖行。 (即:

PS C:\Users\jacob\Documents\Visual Studio 2017\Projects\ShineCalendar\ShineWin\bin\Release> .\InstallAssist.exe -help
PS C:\Users\jacob\Documents\Visual Studio 2017\Projects\ShineCalendar\ShineWin\bin\Release> dir
lation.
Version 0.8.0 (800)
    Directory: C:\Users\jacob\Documents\Visual Studio 2017\Projects\ShineCalendar\ShineWin\bin\Release

Can be accessed through GUI or by using the arguments below:
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         9/9/2016   2:59 PM          77312 CsvHelper.dll 
... (rest omitted)

我该怎么做才能写出输出然后显示整个"PS C:\Users\...\bin\Release>"的内容?

1 个答案:

答案 0 :(得分:1)

您的问题是您的应用程序立即(作为WPF启动代码的一部分)释放控制台。然后cmd.exe准备通过打印提示接受更多输入。

然后,您重新连接到控制台并开始输出帮助文本。但cmd.exe不知道这一点,因此控制台出现乱码。

我建议通过制作两个程序来修复它。按原样保留WPF应用程序,但在前面添加一个小型控制台应用程序。控制台应用程序打印您想要的文本(如果需要),然后启动您的WPF应用程序。