Dim Process As Process Dim Commands As String() Dim TextBox As New TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.TextBox.Multiline = True Me.TextBox.Dock = DockStyle.Fill Me.Controls.Add(TextBox) Commands = New String() {"ping 127.0.0.1", "dir d:\", "dir e:\"} Dim CommandThread As New Thread(New ThreadStart(AddressOf Command)) CommandThread.Start() End Sub
Private Sub Command() Dim psi As New System.Diagnostics.ProcessStartInfo() psi.FileName = System.Environment.GetEnvironmentVariable("ComSpec") psi.RedirectStandardInput = True psi.RedirectStandardOutput = True psi.UseShellExecute = False psi.CreateNoWindow = True Me.Process = Diagnostics.Process.Start(psi) Dim OutputThread As New Thread(New ThreadStart(AddressOf Output)) Dim InputThread As New Thread(New ThreadStart(AddressOf Input)) OutputThread.Start() InputThread.Start() InputThread.Join() OutputThread.Join() Process.WaitForExit() End Sub
Private Sub Output() Dim Result As String = Process.StandardOutput.ReadToEnd Me.Invoke(New AddTextBoxDelegate(AddressOf AddTextBox), New Object() {Result}) End Sub
Private Sub Input() For Each Command As String In Commands Me.Invoke(New AddTextBoxDelegate(AddressOf AddTextBox), New Object() {Command & ControlChars.CrLf}) Process.StandardInput.WriteLine(Command) Next Process.StandardInput.WriteLine("exit") End Sub
Delegate Sub AddTextBoxDelegate(ByVal Text As String)
Private Sub AddTextBox(ByVal Text As String) Me.TextBox.Text &= Text End Sub
Private Sub Input() For Each Command As String In Commands Me.Invoke(New AddTextBoxDelegate(AddressOf AddTextBox), New Object() {Command & ControlChars.CrLf}) ' ↓追加 Process.StandardInput.WriteLine("cd c:\directory") Process.StandardInput.WriteLine(Command) Next Process.StandardInput.WriteLine("exit") End Sub
Private Sub Command() Dim psi As New System.Diagnostics.ProcessStartInfo() psi.FileName = System.Environment.GetEnvironmentVariable("ComSpec") psi.RedirectStandardInput = True psi.RedirectStandardOutput = True psi.RedirectStandardError = True ' ←追加 psi.UseShellExecute = False psi.CreateNoWindow = True Me.Process = Diagnostics.Process.Start(psi) Dim OutputThread As New Thread(New ThreadStart(AddressOf Output)) Dim InputThread As New Thread(New ThreadStart(AddressOf Input)) Dim FailThread As New Thread(New ThreadStart(AddressOf Fail)) OutputThread.Start() InputThread.Start() FailThread.Start() ' ←追加 InputThread.Join() OutputThread.Join() Process.WaitForExit() End Sub
Private Sub Fail() Dim Result As String = Process.StandardError.ReadToEnd Me.Invoke(New AddTextBoxDelegate(AddressOf AddTextBox), New Object() {Result}) End Sub
Private Sub Output() Do While Not Process.StandardOutput.EndOfStream Dim Result As String = Process.StandardOutput.ReadLine & ControlChars.CrLf Me.Invoke(New AddTextBoxDelegate(AddressOf AddTextBox), New Object() {Result}) Loop End Sub
Private Sub Fail() Do While Not Process.StandardOutput.EndOfStream Dim Result As String = Process.StandardError.ReadLine & ControlChars.CrLf Me.Invoke(New AddTextBoxDelegate(AddressOf AddTextBox), New Object() {Result}) Loop End Sub
分類:[.NET]
以下より質問があります。
DOSコマンドを出力しデータを取得する
http://dobon.net/vb/dotnet/process/standardoutput.html
この場合、
psi.Arguments = "/c dir c:\ /w"
これでディレクトリ内のファイル情報が表示されるのですが、
この場合、表示されるファイルの内容が、
いわゆるプロジェクト内のディレクトリがルートになります。
たとえば、以下のようなディレクトリです。
c:\Documents and Setting\User\My Documents\Visual Studio 2005\Project\WindowsApplication\bin\Debug
もしも、c:\上のファイル情報を表示させたい場合、
どのように記述すればいいのでしょうか?
また、たとえば
dirコマンドを実行後、もう一度dirを実行し、Console.WriteLine(results)で
出力結果を書き出したい場合(すなわち、2つのコマンドを順番に実行したい場合)
psi.Arguments = "/c dir c:\ /w"
psi.Arguments = "/c dir c:\ /w"
と、ソース上に2度記述しても1度しか実行されません。
このようなことは出来ないのでしょうか?
よろしくお願いします。