|
' *****************************************************
' 外部実行したアプリの入出力ストリームを処理する
' CMD.EXE が使えると言う事は、コンソールアプリ
' のコントロールは自由
' ※ 他の対話型コンソールアプリに直接行っても良い
' *****************************************************
Imports System.IO
Public Class Form1
Dim myProcess As New Process()
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' *****************************************************
' コマンドプロンプトをコントロールする
' *****************************************************
myProcess.StartInfo.FileName = "cmd.exe"
' *****************************************************
' Shell を使用しないので、この設定でウインドウを非表示
' *****************************************************
myProcess.StartInfo.CreateNoWindow = True
' *****************************************************
' 入・出力ストリームを扱う為の設定
' *****************************************************
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
' *****************************************************
' 開始
' *****************************************************
myProcess.Start()
' *****************************************************
' 出力ストリームを非同期に取得するハンドラの登録
' *****************************************************
AddHandler myProcess.OutputDataReceived, AddressOf OutputHandler
' *****************************************************
' 出力ストリームを非同期取得の開始
' *****************************************************
myProcess.BeginOutputReadLine()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' *****************************************************
' 入力ストリームへコマンドを書き込み
' *****************************************************
myProcess.StandardInput.WriteLine(Me.TextBox1.Text)
End Sub
' *****************************************************
' 出力ストリームを非同期に取得するハンドラ
' *****************************************************
Private Shared Sub OutputHandler(ByVal sendingProcess As Object, _
ByVal outLine As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(outLine.Data) Then
Console.WriteLine(outLine.Data)
End If
End Sub
End Class
| |