【问题标题】:C# app command-line output written to input lineC# app 命令行输出写入输入行
【发布时间】:2018-01-18 06:55:58
【问题描述】:

这可能是一个简单的问题,我只是不知道如何正确地用词大声笑。无论如何,请允许我描述一下我的问题。

所以我编写了一个程序来帮助用户管理他们的程序安装。出于显而易见的原因,我提供了对该应用程序的 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\<b>...</b>\bin\Release&gt;"

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗯...我想这是合理的,不是吗?不过,真的很臭,我有点想把它放在一个应用程序中。不过没关系,我想我会在这方面探索我的选择。
    猜你喜欢
    • 2012-02-18
    • 2010-12-19
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多