このために、3 つのテキストボックスを追加します
1) txterror (コマンド プロンプトまたは DOS からエラーを取得)
2) txtresult (コマンド プロンプトまたは DOS から結果を取得)
3) txtcommand (実行する DOS コマンドを書き込みます)
フォームロード内に以下のコードを記述します
' Set start information.
Dim start_info As New ProcessStartInfo("cmd")
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True
start_info.RedirectStandardInput = True
' Make the process and set its start information.
Dim proc As New Process()
proc.StartInfo = start_info
' Start the process.
proc.Start()
' Attach to stdout and stderr.
Dim std_out As StreamReader = proc.StandardOutput()
Dim SW As System.IO.StreamWriter = proc.StandardInput
Dim std_err As StreamReader = proc.StandardError()
SW.WriteLine(txtcommand.Text)
SW.WriteLine("exit")
' Display the results.
txtresult.Text = std_out.ReadToEnd()
' Clean up.
std_out.Close()
std_err.Close()
proc.Close()
テキストボックスのキーダウン イベント内に以下のコードを記述します (つまり、txtcommand_KeyDown)。
If e.KeyCode = Keys.Enter Then
txterror.Text = ""
' Set start information.
Dim start_info As New ProcessStartInfo("cmd")
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True
start_info.RedirectStandardInput = True
' Make the process and set its start information.
Dim proc As New Process()
proc.StartInfo = start_info
' Start the process.
proc.Start()
' Attach to stdout and stderr.
Dim std_out As StreamReader = proc.StandardOutput
Dim SW As System.IO.StreamWriter = proc.StandardInput
Dim std_err As StreamReader = proc.StandardError()
SW.WriteLine(txtcommand.Text)
SW.WriteLine("exit")
' Display the results.
txtresult.Text = std_out.ReadToEnd()
txterror.Text = std_err.ReadToEnd()
std_err.Dispose()
SW.Close()
' Clean up.
std_out.Close()
std_err.Close()
proc.Close()
End If