Running command line programs from vb.net

G

guy levi

Running command line programs from vb.net and capturing
the output

how do i do this?
 
C

CMG

Below i pasted 2 sub's i stored in a default Module with
things i use often, maybe it's of some use for you.

If it's a CMD.EXE with attributes, you could just pipe it
to a temp file, and read it back after completion....

Public Sub runvisible(ByVal path As String)
Dim myProcess As Process = New Process
Dim s As String
Dim sysFolder As String =
System.Environment.GetFolderPath
(Environment.SpecialFolder.ProgramFiles)
myProcess.StartInfo.FileName = path
myProcess.EnableRaisingEvents = True
myProcess.Start()
Do While Not myProcess.HasExited
Application.DoEvents()
Loop
If Not myProcess.ExitCode = 0 Then
MsgBox("Application Did Not Finish As
Planned...", MsgBoxStyle.OKOnly)
Exit Sub
End If
' Me.Visible = True
myProcess.Close()
End Sub
Public Function runhidden(ByVal path As String, ByVal
arguments As String, ByVal time2wait As Integer)
Dim myProcess As Process = New Process
Dim s As String
If Not arguments = Nothing Then
myProcess.StartInfo.Arguments = arguments
End If
' start the process in a hidden window
myProcess.StartInfo.WindowStyle = _
ProcessWindowStyle.Hidden
myProcess.StartInfo.CreateNoWindow = True
myProcess.EnableRaisingEvents = True
myProcess.Start()
If time2wait = 0 Then
Do While Not myProcess.HasExited
Application.DoEvents()
Loop
Else
' if the process doesn't complete within
' 1 second, kill it
myProcess.WaitForExit(1000)
If Not myProcess.HasExited Then
myProcess.Kill()
End If
End If
If Not myProcess.ExitCode = 0 Then
Return "error"
Exit Function
Else
Return "ok"
End If
' Me.Visible = True
myProcess.Close()
'Me.Dispose()
'End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top