0

I have chosen a console type project in Visual 2010 and want to know how to write to the cmd line.

For example, I want to be able to automatically send a string (like ipconfig) and then have the results displayed in the box.

eg;

Console.WriteLine("ipconfig");        // Send cmd

Thread.Sleep(1000);                   // Sleep for 1s

for (int i = 0; i < 5; i++)           // Display the first 5 lines
        {
          Console.ReadLine();
         }

Edited by buster2209: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Software guy
0

Hi,
Here is the code I wrote, It works, Let me know if you have any questions.

public void cmd(object command)
        {
            try
            {
                string output;
               //First of all define the main process

                System.Diagnostics.ProcessStartInfo Pstart = new System.Diagnostics.ProcessStartInfo("cmd", "/c" + command);

                Pstart.RedirectStandardOutput = true;
                Pstart.UseShellExecute = false;

                System.Diagnostics.Process P = new System.Diagnostics.Process();

                P.StartInfo = Pstart;

                P.Start();
                Thread.Sleep(10000);

                output = P.StandardOutput.ReadToEnd();

                Console.WriteLine("{0}",output);
                 

            }

            catch (Exception e)
            {
                Console.WriteLine("{0}", e.StackTrace);
            
            }
        
        }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.