繁体   English   中英

打印 C# 控制台内容

[英]Printing C# console contents

我目前在控制台屏幕上有一个信息列表 id 喜欢在键入“print”时打印 有没有办法获取控制台的内容并打印文本信息?

您可以将整个内容存储在一个字符串属性中,然后打印出来。

string wholeText;
wholeText += "more text\n";

string userInput = Console.ReadLine();
if (userInput.Equals("print", StringComparisson.OrdinalIgnoreCase))
{
    Console.WriteLine(wholeText);
    //Or export to a file, send to printer...
}

编辑:代码改进

StringBuilder wholeText = new StringBuilder();
wholeText.Append("more text\n");

string userInput = Console.ReadLine();
if (userInput.Equals("print", StringComparisson.OrdinalIgnoreCase))
{
    Console.WriteLine(wholeText.ToString());
    //Or export to a file, send to printer...
}

听起来您需要在这里做两件事:

  1. 捕获标准输出
  2. 将标准输出发送到打印机

要捕获应用程序的标准输出,请探索Console.SetOut方法以输入TextWriter类型,可能是StringWriter

捕获此输出后,您可以使用PrintDocument类打印该流。 示例链接直接从流中读取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM