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
tamc1993
sharnish
mig16
sorry for bothering so much :P
JOLinton
Kwon-il Lee
mig16
Donald Roy Airey
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 =(
Rickalty
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.
noorinuth
I never tested the code and just wrote it out the head.
giannam
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());
}
gblake
" i want to execute cmd commands but do not show cmd... "