1

I have a console application project. In the application I need to start the command prompt and pass an argument to the command prompt.

I have tried

    System.Diagnostics.Process.Start("cmd", "shutdown -s");

but it does nothing but just starts the command prompt

What I want to do is start the command prompt and pass this argument to the command prompt

    "shutdown -s"

How do I do that?.

Rich
  • 5,603
  • 9
  • 39
  • 61
  • 1
    when you post code that you've tried, it's normally useful to tell us what actually happens when you run that code, and why it's now that you want. It's kinda a moot point in this case, but it's a practice that you should cultivate – Sam I am says Reinstate Monica Aug 23 '13 at 19:58
  • Side note: please consider less destructive samples. Also maybe you did and replaced "format c: /y" with "shutdown"... still consider something simple like "echo Done!"... – Alexei Levenkov Aug 23 '13 at 20:15

5 Answers5

3

You actually want to launch that process directly:

Process.Start("shutdown", "-s");
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

use the /C flag

System.Diagnostics.Process.Start("cmd", "/C shutdown -s");
2

Try this:-

ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.Arguments = "/c ping " + machine;
processStartInfo.FileName = "cmd.exe";
Process process = new Process();
process.StartInfo = processStartInfo;
Process.Start("shutdown", "-s");
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Process.Start("shutdown","/s /t 0"); will shut down the machine

crthompson
  • 15,653
  • 6
  • 58
  • 80
1

I use this in my programs. It's run in 'silent mode', which means that there will not be any command window. With this code you can run any program on the computer, the same way as command prompt (CMD).

string programPath = "..."; // e.g "shutdown"
string programArguments = "..."; // e.g. "-s -t 60"
Process p = new Process();
p.StartInfo.UseShellExecute = false;
// set to false or remove line if you want to show the other cmd window
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = programPath;
p.StartInfo.Arguments = programArguments;
p.Start();
Marko Gresak
  • 7,950
  • 5
  • 40
  • 46