|
-
Apr 11th, 2013, 04:08 AM
#1
Thread Starter
Addicted Member
[RESOLVED] cannot get error from commands in dos shell
hi all,
I cannot understand why I cannot visualize error in richtextbox.text reported in dos shell.
this is my example code:
vb.net Code:
Dim start_info As New ProcessStartInfo("cmd.exe")
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True
start_info.RedirectStandardInput = True
Dim proc As New Process()
proc.StartInfo = start_info
proc.Start()
proc.StandardInput.WriteLine("cd|")
proc.StandardInput.close
Dim std_out As StreamReader = proc.StandardOutput()
Dim std_err As StreamReader = proc.StandardError()
RichTextBox1.Text = std_out.ReadToEnd() + std_err.ReadToEnd()
std_out.Close()
std_err.Close()
proc.Close()
I should visualize in richtextbox1.text error message "the syntax of the command is incorrect" meanwhile I don't see anything.
can someone help me please?
thanks in advanced
gio
-
Apr 11th, 2013, 03:04 PM
#2
Re: cannot get error from commands in dos shell
You can't read two streams simultaneously and concatenate them as you're trying to do here. You have to do one then the other. Unfortunately this means that your error messages will be out of order in terms of how you normally see them but it can't be avoided using this method.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 12th, 2013, 02:26 AM
#3
Thread Starter
Addicted Member
Re: cannot get error from commands in dos shell
 Originally Posted by dunfiddlin
You can't read two streams simultaneously and concatenate them as you're trying to do here. You have to do one then the other. Unfortunately this means that your error messages will be out of order in terms of how you normally see them but it can't be avoided using this method.
I tried also using only one stream proc.StandardError without success.
-
Apr 12th, 2013, 03:29 AM
#4
Re: cannot get error from commands in dos shell
Is there a reason you are using the | character in your command? Are you trying to do something else, that you haven't mentioned, that is also not producing the expected output to the RichTextBox?
I ask since changing your command to something like
Code:
proc.StandardInput.WriteLine("cdf")
will produce error text in the RichTextBox as expected.
I never got around to using DOS, so I don't have an answer as to why it's not working with "cd|". However, I believe the | character is the DOS Pipe operator. Could it be that you can't use piping while you are also redirecting standard output/error streams? As an observation, when issuing a command such as
Code:
proc.StandardInput.WriteLine("dir C:\Windows\System32 | MORE")
the | MORE seems to be ignored.
On a related note, should you want to display the output from the STDOUT and STDERR streams "simultaneously" (as opposed to any error messages all being tagged on to the very end of your RichTextBox's Text), then you could use the DOS redirect 2>&1 after your command. This would redirect STDERR to STDOUT before STDOUT is redirected to your RichTextBox. I'm sorry to say that it doesn't help with your original problem, though.
vb.net Code:
Dim start_info As New ProcessStartInfo("cmd.exe")
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
' start_info.RedirectStandardError = True
start_info.RedirectStandardInput = True
Dim proc As New Process()
proc.StartInfo = start_info
proc.Start()
proc.StandardInput.WriteLine("cdf 2>&1")
proc.StandardInput.Close()
Dim std_out As StreamReader = proc.StandardOutput()
' Dim std_err As StreamReader = proc.StandardError()
RichTextBox1.Text = std_out.ReadToEnd() ' + std_err.ReadToEnd()
std_out.Close()
' std_err.Close()
proc.Close()
You don't need to comment out (remove) the StandardError lines, but it might make more sense to do so.
-
Apr 12th, 2013, 04:17 AM
#5
Thread Starter
Addicted Member
Re: cannot get error from commands in dos shell
Hi Inferrd,
no there is no reason to use pipe character, just I wanted to generate a dos command error and capture it in order to show it on richtextbox. So I wrongly decided to use pipe character.
With your help, I solved my issue.
thanks
gio
 Originally Posted by Inferrd
Is there a reason you are using the | character in your command? Are you trying to do something else, that you haven't mentioned, that is also not producing the expected output to the RichTextBox?
I ask since changing your command to something like
Code:
proc.StandardInput.WriteLine("cdf")
will produce error text in the RichTextBox as expected.
I never got around to using DOS, so I don't have an answer as to why it's not working with "cd|". However, I believe the | character is the DOS Pipe operator. Could it be that you can't use piping while you are also redirecting standard output/error streams? As an observation, when issuing a command such as
Code:
proc.StandardInput.WriteLine("dir C:\Windows\System32 | MORE")
the | MORE seems to be ignored.
On a related note, should you want to display the output from the STDOUT and STDERR streams "simultaneously" (as opposed to any error messages all being tagged on to the very end of your RichTextBox's Text), then you could use the DOS redirect 2>&1 after your command. This would redirect STDERR to STDOUT before STDOUT is redirected to your RichTextBox. I'm sorry to say that it doesn't help with your original problem, though.
vb.net Code:
Dim start_info As New ProcessStartInfo("cmd.exe") start_info.UseShellExecute = False start_info.CreateNoWindow = True start_info.RedirectStandardOutput = True ' start_info.RedirectStandardError = True start_info.RedirectStandardInput = True Dim proc As New Process() proc.StartInfo = start_info proc.Start() proc.StandardInput.WriteLine("cdf 2>&1") proc.StandardInput.Close() Dim std_out As StreamReader = proc.StandardOutput() ' Dim std_err As StreamReader = proc.StandardError() RichTextBox1.Text = std_out.ReadToEnd() ' + std_err.ReadToEnd() std_out.Close() ' std_err.Close() proc.Close()
You don't need to comment out (remove) the StandardError lines, but it might make more sense to do so.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|