如何在Windows Service或者Windows Forms输出Console.Write的信息
来源:互联网 发布:淘宝助手详情批量替换 编辑:程序博客网 时间:2024/04/28 08:06
我们很多人都喜欢在一些代码中使用Console.Write或者Console.WriteLine的方式输出一些信息,这种做法,如果用到控制台程序中,是很方便的。但是如果用到了Windows Service或者Windows Forms程序中,那么就不是那么好了,因为它们并没有所谓的Console。(如果我们是在Visual Studio中进行调试的话,会输出到一个Output窗口)
先来看Windows Service的做法。我们可以将这个输出流重定向到一个文件。
StreamWriter sw = new StreamWriter("e://temp//log.log"); sw.AutoFlush = true; Console.SetOut(sw);
而在Windows Forms中呢,当然也可以像上面这样做,但是我们可能希望现在在窗体的一个文本框中,看下面的例子
首先可以编写一个特殊的TextWriter
public class TextBoxWriter : TextWriter { TextBoxBase _textbox; public TextBoxWriter(TextBoxBase textbox) { _textbox = textbox; } public override Encoding Encoding { get { return Encoding.UTF8; } } public override void WriteLine(string value) { base.WriteLine(value); _textbox.AppendText(value + Environment.NewLine); } }
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }然后使用该TextWriter来输出消息到窗体的一个文本框中
TextBoxWriter tw = new TextBoxWriter(richTextBox1); Console.SetOut(tw);
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
当然,如果仅仅是为了做一些跟踪性的输出,则更推荐使用Trace,结合TraceListener来实现。因为Trace是可以有多个输出的,事实上,确实也有一个ConsoleTraceListener可以输出到屏幕上。
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
- 如何在Windows Service或者Windows Forms输出Console.Write的信息
- 如何在当前Eclipse的Console View中输出信息
- 在Console中输出类似Windows系统的日历
- eclipse中如何在运行时工作台(而不是开发工作台)的console中输出信息?
- windows中如何开启一个console来调试信息
- 如何在Windows Forms应用中使用flash
- .NET : 如何在Windows Forms中使用DataRepeater控件
- 如何在VC++中使用Windows Forms (VS2012-VS2015)
- (C#)Windows Forms Service 创建,使用,跟踪
- 如何调试安装在服务器上的Windows Service
- 如何在console mode下顺利编译windows程序
- 如何处理Windows Forms程序中未处理的异常
- 如何处理Windows Forms程序中未处理的异常
- vc6.0或者vs2008 MFC程序采用console输出调试信息的方法
- 在类库中申明"System.Windows.Forms”出错的解决方法
- python3 windows console 输出乱码问题
- 如何在JS中捕获console.log()的输出
- angular2+ 如何在打包发布的时候清除console信息
- 在asp.net页中执行Response.Write("");页面变形的问题
- GCC汇编器研究
- 两个很好玩的网站
- Meshgrid命令详解
- .NET 4中并行编程的简单例子
- 如何在Windows Service或者Windows Forms输出Console.Write的信息
- Bloom Filter
- jQuery学习笔记
- 指针类型转换(非常棒非常全的一篇文章)
- 关于工作流(Workflow Foundation)的一些总结归纳
- 跨还是不跨?这是一个问题
- 宜荡佚简易,宽小过,总大纲而已
- QT输入法浅析
- 如何在不安装Word的情况下生成Word文档