这个例子是从 水如烟的例子 修改过来的。如有不妥之处还请大家指正批评。
Imports System.Threading
Imports System.Runtime.CompilerServices
Public Class frmCmdExcute
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents tbResult As System.Windows.Forms.TextBox
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents btnExcute As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents btClear As System.Windows.Forms.Button
Friend WithEvents tbComText As System.Windows.Forms.ComboBox
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2Me.tbResult = New System.Windows.Forms.TextBox
3Me.Panel1 = New System.Windows.Forms.Panel
4Me.tbComText = New System.Windows.Forms.ComboBox
5Me.btClear = New System.Windows.Forms.Button
6Me.Label1 = New System.Windows.Forms.Label
7Me.btnExcute = New System.Windows.Forms.Button
8Me.Panel1.SuspendLayout()
9Me.SuspendLayout()
10'
11'tbResult
12'
13Me.tbResult.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
14Or System.Windows.Forms.AnchorStyles.Left) _
15Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
16Me.tbResult.BackColor = System.Drawing.SystemColors.Info
17Me.tbResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
18Me.tbResult.Location = New System.Drawing.Point(8, 8)
19Me.tbResult.Multiline = True
20Me.tbResult.Name = "tbResult"
21Me.tbResult.ScrollBars = System.Windows.Forms.ScrollBars.Both
22Me.tbResult.Size = New System.Drawing.Size(584, 304)
23Me.tbResult.TabIndex = 0
24Me.tbResult.Text = ""
25'
26'Panel1
27'
28Me.Panel1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
29Me.Panel1.Controls.Add(Me.tbComText)
30Me.Panel1.Controls.Add(Me.btClear)
31Me.Panel1.Controls.Add(Me.Label1)
32Me.Panel1.Controls.Add(Me.btnExcute)
33Me.Panel1.Location = New System.Drawing.Point(8, 320)
34Me.Panel1.Name = "Panel1"
35Me.Panel1.Size = New System.Drawing.Size(584, 40)
36Me.Panel1.TabIndex = 1
37'
38'tbComText
39'
40Me.tbComText.Location = New System.Drawing.Point(224, 8)
41Me.tbComText.Name = "tbComText"
42Me.tbComText.Size = New System.Drawing.Size(224, 20)
43Me.tbComText.TabIndex = 5
44Me.tbComText.Text = "Dir"
45'
46'btClear
47'
48Me.btClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat
49Me.btClear.Location = New System.Drawing.Point(40, 8)
50Me.btClear.Name = "btClear"
51Me.btClear.Size = New System.Drawing.Size(80, 24)
52Me.btClear.TabIndex = 3
53Me.btClear.Text = "清空(&C)"
54'
55'Label1
56'
57Me.Label1.Location = New System.Drawing.Point(160, 8)
58Me.Label1.Name = "Label1"
59Me.Label1.Size = New System.Drawing.Size(48, 16)
60Me.Label1.TabIndex = 2
61Me.Label1.Text = "命令:"
62'
63'btnExcute
64'
65Me.btnExcute.FlatStyle = System.Windows.Forms.FlatStyle.Flat
66Me.btnExcute.Location = New System.Drawing.Point(472, 8)
67Me.btnExcute.Name = "btnExcute"
68Me.btnExcute.Size = New System.Drawing.Size(80, 24)
69Me.btnExcute.TabIndex = 1
70Me.btnExcute.Text = "执行(&E)"
71'
72'frmCmdExcute
73'
74Me.AcceptButton = Me.btnExcute
75Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
76Me.ClientSize = New System.Drawing.Size(600, 365)
77Me.Controls.Add(Me.Panel1)
78Me.Controls.Add(Me.tbResult)
79Me.Name = "frmCmdExcute"
80Me.Text = "Command Excute Result"
81Me.Panel1.ResumeLayout(False)
82Me.ResumeLayout(False)
83
84End Sub
85
86#End Region
87
88Public Shared Sub Main()
89If System.Environment.OSVersion.ToString.IndexOf("NT") = -1 Then
90MsgBox("暂时不支持非NT系统,程序退出!")
91Application.Exit()
92Else
93Application.Run(New frmCmdExcute)
94End If
95End Sub
96
97Private Delegate Sub TextAddHandler(ByVal strPara As String)
98
99#Region "private viable"
100Dim sw As IO.StreamWriter
101Dim sr As New MyStreamReader
102Dim err As New MyStreamReader
103Dim p As System.Diagnostics.Process = New System.Diagnostics.Process
104Dim psI As New System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("ComSpec"))
105#End Region
106
107Private Sub frmCmdExcute_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
108p = New System.Diagnostics.Process
109'"cmd.exe"为nt的命令行程序
110psI = New System.Diagnostics.ProcessStartInfo("cmd.exe")
111psI.UseShellExecute = False
112psI.RedirectStandardInput = True
113psI.RedirectStandardOutput = True
114psI.RedirectStandardError = True
115psI.CreateNoWindow = True
116p.StartInfo = psI
117
118p.Start()
119sw = p.StandardInput
120sr.stream = p.StandardOutput
121err.stream = p.StandardError
122sw.AutoFlush = True
123sr.stream.BaseStream.BeginRead(sr.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), sr)
124err.stream.BaseStream.BeginRead(err.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), err)
125End Sub
126
127Private Sub frmCmdExcute_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
128p.Close()
129If Not sw Is Nothing Then
130sw.Close()
131End If
132If Not sr Is Nothing Then
133sr.stream.Close()
134End If
135If Not err Is Nothing Then
136err.stream.Close()
137End If
138
139End Sub
140
141Private Sub btClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClear.Click
142Me.tbResult.Text = String.Empty
143End Sub
144
145Private Sub btnExcute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcute.Click
146Me.CmdExcute()
147End Sub
148
149Private Sub CmdExcute()
150Try
151Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
152If Me.tbComText.Text <> "" Then
153sw.WriteLine(Me.tbComText.Text)
154Else
155sw.WriteLine("Dir")
156End If
157Me.Cursor = System.Windows.Forms.Cursors.Default
158
159'向combobox中添加元素
160Me.tbComText.Items.Add(Me.tbComText.Text)
161Me.tbComText.SelectionStart = 0
162Me.tbComText.SelectionLength = Me.tbComText.Text.Length
163Me.tbComText.Focus()
164Catch ex As Exception
165MsgBox("CmdExcute--" & ex.ToString)
166End Try
167End Sub
168
169Private Sub AppText(ByVal strAdd As String)
170Me.tbResult.Text &= strAdd
171End Sub
172
173'回调函数
174'为了保证执行invoke方法的线程顺序,使用了MethodImpl
175<methodimpl(methodimploptions.synchronized)> _
176Sub CBstream(ByVal s As IAsyncResult)
177Try
178Dim t As MyStreamReader = CType(s.AsyncState, MyStreamReader)
179If t.stream.BaseStream Is Nothing Then
180Exit Sub
181End If
182Dim i As Integer = t.stream.BaseStream.EndRead(s)
183Dim strReceive As String = System.Text.Encoding.Default.GetString(t.bytes, 0, i)
184Me.Invoke(New TextAddHandler(AddressOf AppText), New Object() {strReceive})
185t.stream.BaseStream.BeginRead(t.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), t)
186Catch ex As Exception
187MsgBox("CBstream--" & ex.ToString)
188End Try
189End Sub
190
191Friend Class MyStreamReader
192Public stream As IO.StreamReader
193Public bytes(1024) As Byte
194Public Sub New()
195End Sub
196End Class
197End Class</methodimpl(methodimploptions.synchronized)></system.diagnostics.debuggerstepthrough()>