How execute Command Prompt(Ms-DOS) commands through C#
-
I am trying to execute Command Prompt(Ms-DOS) commands through C# like shutdown -s -t 0(and several other commands) by creating an object of System.Diagnostic.Process class. When the Start method is executed a new Command Prompt window opens but the command does not execute.
// code procedure
public static void ExecuteCommand()
{
Process p = new Process("cmd.exe", "shutdown -s -t 0");
p.CreateNoWindow = true;
p.UseShellExecute = false;
p.Start();
}I have imported the System.Diagnostic namespace. Banking establishments are more dangerous than standing armies.
-
I am trying to execute Command Prompt(Ms-DOS) commands through C# like shutdown -s -t 0(and several other commands) by creating an object of System.Diagnostic.Process class. When the Start method is executed a new Command Prompt window opens but the command does not execute.
// code procedure
public static void ExecuteCommand()
{
Process p = new Process("cmd.exe", "shutdown -s -t 0");
p.CreateNoWindow = true;
p.UseShellExecute = false;
p.Start();
}I have imported the System.Diagnostic namespace. Banking establishments are more dangerous than standing armies.
Why do you need to put the program as a parameter? CMD doesn't accept parameters like that. Just replace the line which creates your Process instantiation with
Process p = new Process("shutdown -s -t 0");
That will run the actual program, not just open a command prompt.
OSDev :)
-
Why do you need to put the program as a parameter? CMD doesn't accept parameters like that. Just replace the line which creates your Process instantiation with
Process p = new Process("shutdown -s -t 0");
That will run the actual program, not just open a command prompt.
OSDev :)
Computafreak, The statement you gave gives a file not found exception.
-
I am trying to execute Command Prompt(Ms-DOS) commands through C# like shutdown -s -t 0(and several other commands) by creating an object of System.Diagnostic.Process class. When the Start method is executed a new Command Prompt window opens but the command does not execute.
// code procedure
public static void ExecuteCommand()
{
Process p = new Process("cmd.exe", "shutdown -s -t 0");
p.CreateNoWindow = true;
p.UseShellExecute = false;
p.Start();
}I have imported the System.Diagnostic namespace. Banking establishments are more dangerous than standing armies.
C:\>cmd /?
Starts a new instance of the Windows XP command interpreterCMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]/C Carries out the command specified by string and then terminates
I think you need the /C parameter.
-
Computafreak, The statement you gave gives a file not found exception.
-
I am trying to execute Command Prompt(Ms-DOS) commands through C# like shutdown -s -t 0(and several other commands) by creating an object of System.Diagnostic.Process class. When the Start method is executed a new Command Prompt window opens but the command does not execute.
// code procedure
public static void ExecuteCommand()
{
Process p = new Process("cmd.exe", "shutdown -s -t 0");
p.CreateNoWindow = true;
p.UseShellExecute = false;
p.Start();
}I have imported the System.Diagnostic namespace. Banking establishments are more dangerous than standing armies.
Hi,
Process.Start("shutdown", "-s -t 0");
is all you need assuming you have sufficient rights. shutdown.exe is one of the Windows utilities, no cmd.exe required here. [ADDED] And with an amended 4-line approach like yours, the DOS window flashing can be avoided. [/ADDED] :)Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Sunday, September 27, 2009 2:37 PM