c# – 如何回显现有的CMD窗口

前端之家收集整理的这篇文章主要介绍了c# – 如何回显现有的CMD窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我正在使用的程序可以使用以下代码在CMD中使用命令行启动.
  1. string[] commandLines = Environment.GetCommandLineArgs();@H_502_3@
  2. 但是我希望能够在处理命令行之后将消息返回到CMD窗口.任何帮助,将不胜感激.

  3. 编辑:我将程序作为Windows应用程序运行,而不是控制台应用程序.

解决方法

我最后使用RenniePet发布的答案作为对我的问题的评论解决问题.我会在这里列出解决方案,以便任何试图重现它的人.
  1. [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  2. private static extern bool AttachConsole(int dwProcessId);
  3.  
  4. private const int ATTACH_PARENT_PROCESS = -1;
  5.  
  6. StreamWriter _stdOutWriter;
  7.  
  8. // this must be called early in the program
  9. public void GUIConsoleWriter()
  10. {
  11. // this needs to happen before attachconsole.
  12. // If the output is not redirected we still get a valid stream but it doesn't appear to write anywhere
  13. // I guess it probably does write somewhere,but nowhere I can find out about
  14. var stdout = Console.OpenStandardOutput();
  15. _stdOutWriter = new StreamWriter(stdout);
  16. _stdOutWriter.AutoFlush = true;
  17.  
  18. AttachConsole(ATTACH_PARENT_PROCESS);
  19. }
  20.  
  21. public void WriteLine(string line)
  22. {
  23. GUIConsoleWriter();
  24. _stdOutWriter.WriteLine(line);
  25. Console.WriteLine(line);
  26. }@H_502_3@
  27. 将此代码添加到程序后,您只需使用例如以下内容开始返回行.

  28. WriteLine("\nExecuting commands.");@H_502_3@

猜你在找的C#相关文章