Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How execute Command Prompt(Ms-DOS) commands through C#

How execute Command Prompt(Ms-DOS) commands through C#

Scheduled Pinned Locked Moved C#
csharp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DarkSorrow38
    wrote on last edited by
    #1

    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.

    0 P L 3 Replies Last reply
    0
    • D DarkSorrow38

      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.

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      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 :)

      D 1 Reply Last reply
      0
      • 0 0x3c0

        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 :)

        D Offline
        D Offline
        DarkSorrow38
        wrote on last edited by
        #3

        Computafreak, The statement you gave gives a file not found exception.

        0 1 Reply Last reply
        0
        • D DarkSorrow38

          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.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          C:\>cmd /?
          Starts a new instance of the Windows XP command interpreter

          CMD [/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.

          1 Reply Last reply
          0
          • D DarkSorrow38

            Computafreak, The statement you gave gives a file not found exception.

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #5

            That's odd. Try just invoking Process.Start(...) instead of manually instantiating the Process class.

            OSDev :)

            1 Reply Last reply
            0
            • D DarkSorrow38

              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.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups