![](/img/trans.png)
[英]Simple method for accepting command-line input for a standard Windows service written in C#?
[英]C# app command-line output written to input line
这可能是一个简单的问题,我只是没有弄清楚如何正确地大声笑。 无论如何,请允许我描述我的问题。
因此,我编写了一个程序,可以帮助用户管理其程序的安装。 出于明显的原因,我提供了对此应用程序的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>"
?
您的问题是您的应用程序立即(作为WPF启动代码的一部分)释放了控制台。 然后,cmd.exe准备通过打印提示来接受更多输入。
然后,您重新连接到控制台并开始输出帮助文本。 但是cmd.exe对此不了解,因此控制台会出现乱码。
我建议通过制作两个程序来修复它。 保持WPF应用不变,但在前面添加一个小型控制台应用。 控制台应用程序将打印所需的文本(如果需要),然后启动WPF应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.