hi ppl.. i have a prgram that needs to be able to emulate console (cmd) commands... i want to execute cmd commands but do not show cmd... for example... run the command dir.. and retreive the result without using the Run() method. .. cause it shows the cmd,, is there any way to do this
thx
mig16

Console commands
Maurycy Markowski - Microsoft
Joe H
I never tested the code and just wrote it out the head.
Jon_DEV
" i want to execute cmd commands but do not show cmd... "
Dwayne Need [MSFT]
try
{
ProcessStartInfo info = new ProcessStartInfo("cmd.exe","netstat -n");
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
Process process = Process.Start(info);
using( StreamReader reader = process.StandardOutput )
{
while( !process.HasExited )
{
string line = reader.ReadLine();
Debug.WriteLine( line, "Console output" );
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Or when you use netstat only once:
try
{
ProcessStartInfo info = new ProcessStartInfo("cmd.exe","netstat -n");
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
Process process = Process.Start(info);
process.WaitForExit();
using( StreamReader reader = process.StandardOutput )
{
string fullOuput = reader.ReadToEnd();
Debug.WriteLine( fullOuput, "Console output" );
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Dr. Flye
mig16
Enrico Sicignano
mig16
sorry for bothering so much :P
Zulfiqar
SHRIMANT PATEL
try
{
ProcessStartInfo info = new ProcessStartInfo("cmd.exe","netstat -n");
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
Process process = Process.Start(info);
System.IO.StreamReader rdr = process.StandardOutput;
int count = rdr.Read();
char[] text = new char[count];
rdr.Read(text, 0, text.Length);
rdr.Close();
string x = null;
foreach (char c in text)
{
x = x + c;
}
Console.WriteLine(x);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
but my result is this:
Microsoft Windows XP [Version 5.1.2600]... <--- this allways shows up when u open the cmd,,, how can i get the result of the command netstat - n or maybe dir =(
thangnc
ProcessStartInfo info = new ProcessStartInfo( @"cmd" );
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start( info );
Use the StandardInput stream property to send command and StandardOutput stream to read the responses.