这个例子是从 水如烟的例子 修改过来的。如有不妥之处还请大家指正批评。
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
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
me.tbresult = new system.windows.forms.textbox
me.panel1 = new system.windows.forms.panel
me.tbcomtext = new system.windows.forms.combobox
me.btclear = new system.windows.forms.button
me.label1 = new system.windows.forms.label
me.btnexcute = new system.windows.forms.button
me.panel1.suspendlayout()
me.suspendlayout()
'
'tbresult
'
me.tbresult.anchor = ctype((((system.windows.forms.anchorstyles.top or system.windows.forms.anchorstyles.bottom) _
or system.windows.forms.anchorstyles.left) _
or system.windows.forms.anchorstyles.right), system.windows.forms.anchorstyles)
me.tbresult.backcolor = system.drawing.systemcolors.info
me.tbresult.borderstyle = system.windows.forms.borderstyle.fixedsingle
me.tbresult.location = new system.drawing.point(8, 8)
me.tbresult.multiline = true
me.tbresult.name = "tbresult"
me.tbresult.scrollbars = system.windows.forms.scrollbars.both
me.tbresult.size = new system.drawing.size(584, 304)
me.tbresult.tabindex = 0
me.tbresult.text = ""
'
'panel1
'
me.panel1.anchor = ctype((system.windows.forms.anchorstyles.bottom or system.windows.forms.anchorstyles.right), system.windows.forms.anchorstyles)
me.panel1.controls.add(me.tbcomtext)
me.panel1.controls.add(me.btclear)
me.panel1.controls.add(me.label1)
me.panel1.controls.add(me.btnexcute)
me.panel1.location = new system.drawing.point(8, 320)
me.panel1.name = "panel1"
me.panel1.size = new system.drawing.size(584, 40)
me.panel1.tabindex = 1
'
'tbcomtext
'
me.tbcomtext.location = new system.drawing.point(224, 8)
me.tbcomtext.name = "tbcomtext"
me.tbcomtext.size = new system.drawing.size(224, 20)
me.tbcomtext.tabindex = 5
me.tbcomtext.text = "dir"
'
'btclear
'
me.btclear.flatstyle = system.windows.forms.flatstyle.flat
me.btclear.location = new system.drawing.point(40, 8)
me.btclear.name = "btclear"
me.btclear.size = new system.drawing.size(80, 24)
me.btclear.tabindex = 3
me.btclear.text = "清空(&c)"
'
'label1
'
me.label1.location = new system.drawing.point(160, 8)
me.label1.name = "label1"
me.label1.size = new system.drawing.size(48, 16)
me.label1.tabindex = 2
me.label1.text = "命令:"
'
'btnexcute
'
me.btnexcute.flatstyle = system.windows.forms.flatstyle.flat
me.btnexcute.location = new system.drawing.point(472, 8)
me.btnexcute.name = "btnexcute"
me.btnexcute.size = new system.drawing.size(80, 24)
me.btnexcute.tabindex = 1
me.btnexcute.text = "执行(&e)"
'
'frmcmdexcute
'
me.acceptbutton = me.btnexcute
me.autoscalebasesize = new system.drawing.size(6, 14)
me.clientsize = new system.drawing.size(600, 365)
me.controls.add(me.panel1)
me.controls.add(me.tbresult)
me.name = "frmcmdexcute"
me.text = "command excute result"
me.panel1.resumelayout(false)
me.resumelayout(false)
end sub
#end region
public shared sub main()
if system.environment.osversion.tostring.indexof("nt") = -1 then
msgbox("暂时不支持非nt系统,程序退出!")
application.exit()
else
application.run(new frmcmdexcute)
end if
end sub
private delegate sub textaddhandler(byval strpara as string)
#region "private viable"
dim sw as io.streamwriter
dim sr as new mystreamreader
dim err as new mystreamreader
dim p as system.diagnostics.process = new system.diagnostics.process
dim psi as new system.diagnostics.processstartinfo(system.environment.getenvironmentvariable("comspec"))
#end region
private sub frmcmdexcute_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
p = new system.diagnostics.process
'"cmd.exe"为nt的命令行程序
psi = new system.diagnostics.processstartinfo("cmd.exe")
psi.useshellexecute = false
psi.redirectstandardinput = true
psi.redirectstandardoutput = true
psi.redirectstandarderror = true
psi.createnowindow = true
p.startinfo = psi
p.start()
sw = p.standardinput
sr.stream = p.standardoutput
err.stream = p.standarderror
sw.autoflush = true
sr.stream.basestream.beginread(sr.bytes, 0, 1024, new asynccallback(addressof cbstream), sr)
err.stream.basestream.beginread(err.bytes, 0, 1024, new asynccallback(addressof cbstream), err)
end sub
private sub frmcmdexcute_closed(byval sender as object, byval e as system.eventargs) handles mybase.closed
p.close()
if not sw is nothing then
sw.close()
end if
if not sr is nothing then
sr.stream.close()
end if
if not err is nothing then
err.stream.close()
end if
end sub
private sub btclear_click(byval sender as system.object, byval e as system.eventargs) handles btclear.click
me.tbresult.text = string.empty
end sub
private sub btnexcute_click(byval sender as system.object, byval e as system.eventargs) handles btnexcute.click
me.cmdexcute()
end sub
private sub cmdexcute()
try
me.cursor = system.windows.forms.cursors.waitcursor
if me.tbcomtext.text <> "" then
sw.writeline(me.tbcomtext.text)
else
sw.writeline("dir")
end if
me.cursor = system.windows.forms.cursors.default
'向combobox中添加元素
me.tbcomtext.items.add(me.tbcomtext.text)
me.tbcomtext.selectionstart = 0
me.tbcomtext.selectionlength = me.tbcomtext.text.length
me.tbcomtext.focus()
catch ex as exception
msgbox("cmdexcute--" & ex.tostring)
end try
end sub
private sub apptext(byval stradd as string)
me.tbresult.text &= stradd
end sub
'回调函数
'为了保证执行invoke方法的线程顺序,使用了methodimpl
<methodimpl(methodimploptions.synchronized)> _
sub cbstream(byval s as iasyncresult)
try
dim t as mystreamreader = ctype(s.asyncstate, mystreamreader)
if t.stream.basestream is nothing then
exit sub
end if
dim i as integer = t.stream.basestream.endread(s)
dim strreceive as string = system.text.encoding.default.getstring(t.bytes, 0, i)
me.invoke(new textaddhandler(addressof apptext), new object() {strreceive})
t.stream.basestream.beginread(t.bytes, 0, 1024, new asynccallback(addressof cbstream), t)
catch ex as exception
msgbox("cbstream--" & ex.tostring)
end try
end sub
friend class mystreamreader
public stream as io.streamreader
public bytes(1024) as byte
public sub new()
end sub
end class
end class