vb.net Windows Forms to implement DOS commands

Source: Internet
Author: User
Tags exit empty implement tostring
Window This example is modified from the example of a water smoke. If there are any irregularities, please correct the criticism.


Imports System.Threading
Imports System.Runtime.CompilerServices
Public Class Frmcmdexcute
Inherits System.Windows.Forms.Form

#Region "code generated by the Windows forms Designer"
Public Sub New ()

MyBase.New ()

' This call is required by the Windows Forms Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' Form overrides dispose to clean up the list of components.

Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub

' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not modify it using the Code Editor.

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 = "Empty (&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 = "Command:"
'
' 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 = "Execute (&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 ("Do not support non-NT system temporarily, program exits!") ")
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 ' is a command-line program for 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 are nothing Then
Sw. Close ()
End If
If not SR are nothing Then
Sr.stream.Close ()
End If
If not err are 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

' Add elements to 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

' Callback function
' To ensure the thread order of the Invoke method is executed, the MethodImpl is used
<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





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.