国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

vb.net的windows窗體實現dos命令

2024-07-10 13:00:52
字體:
來源:轉載
供稿:網友


這個例子是從 水如煙的例子 修改過來的。如有不妥之處還請大家指正批評。


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



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武山县| 化州市| 永仁县| 普定县| 荆门市| 浦东新区| 芷江| 筠连县| 甘德县| 南昌市| 宁津县| 潼关县| 惠来县| 遂川县| 兴国县| 方正县| 两当县| 大荔县| 潞城市| 玛沁县| 新绛县| 凭祥市| 乌审旗| 丰都县| 探索| 交口县| 利辛县| 临颍县| 合川市| 栾川县| 望城县| 泰顺县| 乌拉特中旗| 阳原县| 达日县| 体育| 正镶白旗| 顺昌县| 柞水县| 若尔盖县| 满洲里市|