Console commands

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


Answer this question

Console commands

  • Maurycy Markowski - Microsoft

    I have tested it now, but i don't receive the full output iether. Check this example at GotDotNet: netstat in C#.

  • Joe H

    Use my first code sample and when the process is idle and you received all information just kill the process and it stops running. Then code code will continue.

    I never tested the code and just wrote it out the head.


  • Jon_DEV

    thanks but i want this:

    " i want to execute cmd commands but do not show cmd... "

  • Dwayne Need [MSFT]

    You need to read until the process has exited:


    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

    mmm ... what am i doing wrong =(.... i copy / pasted ur coden in my main method and my window stays opened.,,, with no result....

    mig16

  • Enrico Sicignano

    hehe the problem is that i never received lal the information... the closest i was i just received the "Welcome message to cmd from microsoft" the 1 taht says Microsoft windows xp [5.1.xxx] ... that was the closest...

    mig16

    sorry for bothering so much :P

  • Zulfiqar

    Check the Process and the ProcessStartInfo classes.


  • SHRIMANT PATEL

    thanks,, this is my code:


    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.


  • Console commands