passing Console output to a form application

  • Thread starter Thread starter gab
  • Start date Start date
G

gab

I want to execute a command-line-command in my vb.net application so
that i can handle it in my programm.

Can someone tell me how to do this.
example: lets execute the "dir"-command and write the output to a
richttextbox. does someone know how this work?
 
You can do this with the Process class (http://tinyurl.com/2j73y). You can
use the StandardOutput property to get text sent to the output
(http://tinyurl.com/2a36b).

Remarks
In order to use StandardOutput, you must have specified true for the
StartInfo property's RedirectStandardOutput property. Otherwise, reading the
StandardOutput property throws an exception.

Note UseShellExecute on the StartInfo property must be false if you
want to set StandardOutput to true.
The Process component communicates with a child process using a pipe. If a
child process writes enough data to the pipe to fill the buffer, the child
will block until the parent reads the data from the pipe. This can cause
deadlock if your application is reading all output to standard error and
standard output. The following C# code, for example, could be problematic.
 
would you be so good and give me a short code example. i quite new to
the .net thing and so its not easy. thx.
 
To start a process:
Dim p As New Process
p.Start("calc.exe")

For more info see MSDN, there are some examples!
 
complete example
ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
psi.WindowStyle = ProcessWindowStyle.Hidden;

Process p = new Process();
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(MyExited);
p.StartInfo = psi;
p.Start();

..... do stuff ...

p.Kill(); // Try killing the process

private void MyExited(object sender, EventArgs e)
{
MessageBox.Show("Exited process");
}

This will run notepad "hidden" (for illustration purposes).. Then p.Kill();
should kill it. I haven't tried the Kill method. YMMV.
 
Back
Top