473,387 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Unable to Read the total Output from the Command Prompt

36
Hi,
I am working on C# windows applications.What i want to do is, I want to Redirect the output from Command Prompt to the Rich Text Box, Actually i am unable to read the entire output of the comman prompt, but i can see only the few lines.I don't know what's the wrong in my code.Here is the sample code.
ChildLaunch()
{
Process p = new Process();
p.StartInfo.WorkingDirectory = RootDir;
p.StartInfo.FileName = Environment.GetEnvironmentVariable("COMSPEC");
p.StartInfo.Arguments = "/c" + " " + CommandLine.Trim();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.EnableRaisingEvents = true;
p.Start();
p.BeginOutputReadLine();
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
richTextBox1.AppendText(e.Data);
}


Here RootDir is the working directory which we have to set in the command prompt. CommandLine is the String which i used in my code to collect all the elements from the GUI.
Using my code I can read only some amount of output.. But i need to read the entire output. Can any one help me?
Apr 1 '08 #1
4 1968
r035198x
13,262 8TB
Hi,
I am working on C# windows applications.What i want to do is, I want to Redirect the output from Command Prompt to the Rich Text Box, Actually i am unable to read the entire output of the comman prompt, but i can see only the few lines.I don't know what's the wrong in my code.Here is the sample code.
ChildLaunch()
{
Process p = new Process();
p.StartInfo.WorkingDirectory = RootDir;
p.StartInfo.FileName = Environment.GetEnvironmentVariable("COMSPEC");
p.StartInfo.Arguments = "/c" + " " + CommandLine.Trim();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.EnableRaisingEvents = true;
p.Start();
p.BeginOutputReadLine();
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
richTextBox1.AppendText(e.Data);
}


Here RootDir is the working directory which we have to set in the command prompt. CommandLine is the String which i used in my code to collect all the elements from the GUI.
Using my code I can read only some amount of output.. But i need to read the entire output. Can any one help me?
After p.Start(), do
Expand|Select|Wrap|Line Numbers
  1. String output = p.StandardOutput.ReadToEnd();
You can then send that output wherever you want.
Apr 1 '08 #2
sdanda
36
Hi,
I already tried by placing that statement but even after giving that statement i am unable to read the complete output

The sample code with the above statement is
delegate declaration:-
delegate void WriteToRichTextBox(string Msg);

Thread declaration:-
Thread t=new Thread(new ThreadStart(ChildLaunch);
t.Start();


void ChildLauch()
{
Process p = new Process();
p.StartInfo.WorkingDirectory = RootDir;
p.StartInfo.FileName = Environment.GetEnvironmentVariable("COMSPEC");
p.StartInfo.Arguments = "/c" + " " + CommandLine.Trim();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.EnableRaisingEvents = true;
p.Start();
richTextBox1.Invoke(new WriteToRichTextBox(WriteText),new object[] {p.StandardOutput.ReadToEnd().ToString()});
p.Close();
}

public void WriteText(string msg)
{
richTextBox1.AppendText(msg);
}

Here even if i use synchronous ReadToEnd() function i am unable to see the total Command Line Output
Apr 1 '08 #3
r035198x
13,262 8TB
Hi,
I already tried by placing that statement but even after giving that statement i am unable to read the complete output

The sample code with the above statement is
delegate declaration:-
delegate void WriteToRichTextBox(string Msg);

Thread declaration:-
Thread t=new Thread(new ThreadStart(ChildLaunch);
t.Start();


void ChildLauch()
{
Process p = new Process();
p.StartInfo.WorkingDirectory = RootDir;
p.StartInfo.FileName = Environment.GetEnvironmentVariable("COMSPEC");
p.StartInfo.Arguments = "/c" + " " + CommandLine.Trim();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.EnableRaisingEvents = true;
p.Start();
richTextBox1.Invoke(new WriteToRichTextBox(WriteText),new object[] {p.StandardOutput.ReadToEnd().ToString()});
p.Close();
}

public void WriteText(string msg)
{
richTextBox1.AppendText(msg);
}

Here even if i use synchronous ReadToEnd() function i am unable to see the total Command Line Output
What if you use
p.WaitForExit(WithAMillsecondsTimeOut); ?
Apr 1 '08 #4
Plater
7,872 Expert 4TB
Have you checked to see if there is output on the Standard Error stream?
Apr 1 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Wayno | last post by:
Hoping someone can help me out with this problem. First problem, is I am unable to view my php error log. In my php.ini I have my log in /var/www/logs/php_error_log. However, it has remained empty....
2
by: Mustaq | last post by:
Hi, I have an application in VB.NET which runs from command prompt, I need output message in the same command prompt window, Example D:\Mustaq\ORC\Orc 3.7\Insertor\Insertor\bin>insertor.exe ...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
3
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
1
by: Div / | last post by:
I have a very strange problem here. I have a wrapper class for a command line MyProgram.exe. When i run it from the command prompt (MyProgram.exe), it runs perfectly. So there is not problem with...
5
by: danish | last post by:
I download the oci-1.2.2.tgz file and generated the oci8.so file. After adding the extension=oci8.so in php.ini I get the error unable to load dynamic library /usr/lib/php4/oci8.so
3
by: =?Utf-8?B?Q0QuU21hbGxleQ==?= | last post by:
Is there a command line switch available for the EventViewer which will allow me to specify which directory to view the available logs from? I will have multiple workstations writing to a...
6
Cintury
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
2
by: dudeja.rajat | last post by:
On Mon, Sep 8, 2008 at 11:50 AM, <dudeja.rajat@gmail.comwrote: Ok, I re-phrase my question: there is a batch file that executes a exe file. The batch just works if run from command prompt and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.