本文以一个简单的小例子讲解如何将命令行信息实时的输出到文本框中。仅供学习分享使用,如有不足之处,还请指正。
概述
在C#程序开发过程中,有时需要运行其它的程序并获得输出的结果来进行进一步的处理。一般第三方的程序,主要通过进程来调用,如果能够获取第三方程序执行过程中的信息,就显得方便而有用。
涉及知识点:
- 进程相关类: Process,ProcessStartInfo,主要设置进程的重定向输出,以及接受数据的事件。
- 文本框操作:多行显示,滚动条总在最下面
效果图
如下【如果命令执行完毕,会自动结束,如果中断进程,可以手动点击结束进程】:
核心代码
主要代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace DemoBat { public partial class MainForm : Form { private BatStatus curBatSataus = BatStatus.NONE; private Process curProcess = new Process(); public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { InitInfo(); } private void InitInfo() { curProcess.OutputDataReceived -= new DataReceivedEventHandler(ProcessOutDataReceived); ProcessStartInfo p = new ProcessStartInfo(); p.FileName = "cmd.exe"; //p.Arguments = " -t 192.168.1.103"; p.UseShellExecute = false; p.WindowStyle = ProcessWindowStyle.Hidden; p.CreateNoWindow = true; p.RedirectStandardError = true; p.RedirectStandardInput = true; p.RedirectStandardOutput = true; curProcess.StartInfo = p; curProcess.Start(); curProcess.BeginOutputReadLine(); curProcess.OutputDataReceived += new DataReceivedEventHandler(ProcessOutDataReceived); } /// <summary> /// 开始命令行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtCommand.Text.Trim())) { MessageBox.Show("请输入命令"); } if (curBatSataus != BatStatus.ON) { curProcess.StandardInput.WriteLine(this.txtCommand.Text.Trim()); curBatSataus = BatStatus.ON; } else { MessageBox.Show("当前进程正在运行,请先关闭"); } } /// <summary> /// 结束命令行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStop_Click(object sender, EventArgs e) { if (curBatSataus == BatStatus.ON) { curProcess.CancelOutputRead();//取消异步操作 curProcess.Kill(); curBatSataus = BatStatus.OFF; //如果需要手动关闭,则关闭后再进行初始化 InitInfo(); } } /// <summary> /// 进程接受事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void ProcessOutDataReceived(object sender, DataReceivedEventArgs e) { if (this.txtOutPutInfo.InvokeRequired) { this.txtOutPutInfo.Invoke(new Action(() => { this.txtOutPutInfo.AppendText(e.Data + "rn"); })); } else { this.txtOutPutInfo.AppendText(e.Data + "rn"); } } private void timer1_Tick(object sender, EventArgs e) { if ((string.IsNullOrEmpty(this.curProcess.StartInfo.FileName) || this.curProcess.StandardInput.BaseStream.CanWrite) && curBatSataus != BatStatus.OFF) { curBatSataus = BatStatus.OFF; } } } /// <summary> /// 命令状态 /// </summary> public enum BatStatus { NONE = 0, ON = 1, OFF = 2 } }
备注:
关于如何在命令行执行过程中【如:Ping 192.168.1.100 -t】,键入快捷键【如:Ctrl+C】等操作,目前还没有实现。目前采用的就强制关闭进程方法
源码下载
以上就是C# 动态输出Dos命令执行结果的实例(附源码)的详细内容,更多关于C# 动态输出Dos命令执行结果的资料请关注靠谱客其它相关文章!
最后
以上就是聪明煎饼最近收集整理的关于C# 动态输出Dos命令执行结果的实例(附源码)的全部内容,更多相关C#内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复