I’m trying to run dos commands within vb.net program and capture output. I have the following code:
Dim CMDServer As Diagnostics.ProcessStartInfo
Dim CMDReply As Diagnostics.Process
CMDServer = New Diagnostics.ProcessStartInfo
CMDServer.FileName = "cmd.exe"
CMDServer.UseShellExecute = False
CMDServer.RedirectStandardOutput = True
CMDServer.CreateNoWindow = True
CMDServer.Arguments = "/C " + command
CMDReply = Process.Start(CMDServer)
Dim Reply As String = CMDReply.StandardOutput.ReadToEnd()
The code runs successfully if command is a valid dos command, and I get the output in Reply. If the command have no output ( eg: cd\ ) Reply is null. The problem is Reply is null even when the command is invalid. How to capture errors like “command is not recognized as an internal or external command…”, “The system cannot find the path specified..” etc.. Please help me. Thanks..
Error messages come in a different output stream called
StandardError. Just use a StreamReader or read it directly. Of course, theRedirectStandardError-Property of yourProcessStartInfoinstance must be set toTrue.Also, there is a
ExitCode-Property which returns the ExitCode of the program after it has finished.0means ‘successful’. Other error codes can be found in the MSDN Documentation. Here is a list of the common exit codes. For example,2meansThe system cannot find the file specified..