thanks in advance i just want to know if theres a way to read the output from a running batch file in a vb.net program. Thanks!
Asked
Active
Viewed 1,884 times
0
-
1need more information – Fredou Jan 01 '15 at 18:39
-
Batch file is actually ambiguous, but I think it's safe to assume you mean a batch file (.bat) for the command console/window (cmd.exe). and by output you mean.. printed to screen, right?.. – Brett Caswell Jan 01 '15 at 19:07
-
possible duplicate of [How do I embed a batch file in a vb program?](http://stackoverflow.com/questions/27727564/how-do-i-embed-a-batch-file-in-a-vb-program) – The Blue Dog Jan 01 '15 at 19:08
-
doesn't seem like a duplicate of the question.. – Brett Caswell Jan 01 '15 at 19:08
-
however, it is a duplicate of this SO question: http://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c – Brett Caswell Jan 01 '15 at 19:09
-
Is it already running separately? Is it launched from the .NET program? – Jeff B Jan 01 '15 at 19:19
-
It is running Separately – John Jan 01 '15 at 19:20
-
you can let the batch output to be in a txt file and then you can read the txt file that contains the output – Monah Jan 01 '15 at 20:16
1 Answers
0
As one commenter above mentions, you can run the batch file in a shell within your VB.NET program and then read the directed output. I have done this exactly in previous project.
Here is a code snippet which shows how you can do it:
Dim outputFile As String = """" & Path.GetTempFileName & """"
Dim batchCommand As String = """C:\Path\To\MyFile.bat"">" & outputFile
Dim cmdProcess As New Process
With cmdProcess
.StartInfo = New ProcessStartInfo("cmd.exe", "/C " & batchCommand)
With .StartInfo
.CreateNoWindow = True
.UseShellExecute = False
End With
.Start()
.WaitForExit()
End With
' This is the output from the batch file.
Dim batchOutput As String = My.Computer.FileSystem.ReadAllText(outputFile)

Jason Faulkner
- 6,378
- 2
- 28
- 33