Get Result OF Console

  • Thread starter Thread starter Mr Mind - Lion
  • Start date Start date
M

Mr Mind - Lion

Is there any way in .NET to force to run console application through
it and get the result of tht application in our program.

Please reply me quickly.
Any reply will highly be appreciated.
Thanx.
 
This may be a little rough, but I've used the code below to launch a console
application and have the output displayed on a VB.NET form called
"fMainForm" in a textbox called "txtLog".

The "WaitForExe" sub routine launches the specified application, and the
"hide" variable determines if the console application will be shown or
hidden. If the application is hidden, the "monitorProc" sub routine is
called to redirect the standard output for the application so that the
VB.NET application can receive all of the information. Otherwise, the main
VB application is hidden until the console app completes.

I used this mainly with "makecab.exe" so that I could display the progress
of the cabinet creation process without having the console application being
seen by the user. Maybe this will give you some idea of how to accomplish
the task....


' Launch a process and wait for it to exit
Public Function WaitForExe(ByVal fileName As String, ByVal args As
String, ByVal wDir As String, Optional ByVal hide As Boolean = True)
Dim startinfo As New ProcessStartInfo()
startinfo.FileName = fileName
If args <> "" Then
startinfo.Arguments = args
End If

If hide Then
startinfo.RedirectStandardOutput = True
startinfo.CreateNoWindow = True
startinfo.UseShellExecute = False
End If

If wDir <> "" Then
startinfo.WorkingDirectory = wDir
End If

Dim myproc As Process = Process.Start(startinfo)

Dim StdOut As IO.StreamReader = Nothing
If hide Then
StdOut = myproc.StandardOutput
End If
Application.DoEvents()
System.Threading.Thread.Sleep(100)
If hide Then
monitorProc(myproc, StdOut)
Else
fMainForm.ShowInTaskbar = False
fMainForm.Hide()
myproc.WaitForExit(Int32.MaxValue)
fMainForm.ShowInTaskbar = True
fMainForm.Show()
End If
End Function

' Monitor a process
Private Function monitorProc(ByVal myproc As Process, ByVal StdOut As
IO.StreamReader)
Dim g As Drawing.Graphics =
Drawing.Graphics.FromHwnd(fMainForm.txtLog.Handle)
Try
Dim iPeek As Integer = StdOut.Peek
While iPeek > 0
Dim cBuff(iPeek) As Char
Dim str As String = StdOut.ReadLine
Dim pos As Integer = str.IndexOf(Chr(13))
While pos > -1
Dim ts As String
If str.Length > pos + 1 Then
ts = str.Substring(0, pos) & vbCrLf &
str.Substring(pos + 1)
Else
ts = str.Substring(0, pos) & vbCrLf
End If
str = ts
If str.Length > pos + 1 Then
pos = str.IndexOf(Chr(13), pos + 1)
Else
pos = -1
End If
End While
g.FillRectangle(Drawing.SystemBrushes.Control, 2,
fMainForm.txtLog.Height - 20, fMainForm.txtLog.Width - 8, 23)
g.DrawString(str, fMainForm.txtLog.Font,
Drawing.SystemBrushes.ControlText, 0, fMainForm.txtLog.Height - 25)
iPeek = StdOut.Peek
End While
g.FillRectangle(Drawing.SystemBrushes.Control, 2,
fMainForm.txtLog.Height - 20, fMainForm.txtLog.Width - 8, 23)
Catch exc As Exception
Debug.WriteLine(exc.Message)
End Try
End Function
 
Be more specific !

OHM
Is there any way in .NET to force to run console application through
it and get the result of tht application in our program.

Please reply me quickly.
Any reply will highly be appreciated.
Thanx.
 
Back
Top