30

In C# WPF: I want to execute a CMD command, how exactly can I execute a cmd command programmatically?

Jake
  • 783
  • 5
  • 12
  • 18
  • i don't know much about c, c++, or c#, but i would recommend programming it to write the code into a batch file, run the batch file, and then delete the batch file. – Johnny G Gaming Mar 11 '17 at 03:04

10 Answers10

38

Here's a simple example :

Process.Start("cmd","/C copy c:\\file.txt lpt1");
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • 3
    I'm trying that, but the second parameter, the argument is not really being passed to the command window, at least not in Windows 8.1 – William Jan 21 '16 at 21:45
  • @William I tested this in windows 10 and it works correctly. – M at Aug 02 '16 at 04:52
  • dear @jan'splitek I'm 100% sure about this code . can you open cmd using run ? (your local variables may damaged) – M at Mar 23 '17 at 06:50
22

As mentioned by the other answers you can use:

  Process.Start("notepad somefile.txt");

However, there is another way.

You can instance a Process object and call the Start instance method:

  Process process = new Process();
  process.StartInfo.FileName = "notepad.exe";
  process.StartInfo.WorkingDirectory = "c:\temp";
  process.StartInfo.Arguments = "somefile.txt";
  process.Start();

Doing it this way allows you to configure more options before starting the process. The Process object also allows you to retrieve information about the process whilst it is executing and it will give you a notification (via the Exited event) when the process has finished.

Addition: Don't forget to set 'process.EnableRaisingEvents' to 'true' if you want to hook the 'Exited' event.

Ashley Davis
  • 9,896
  • 7
  • 69
  • 87
11

if you want to start application with cmd use this code:

string YourApplicationPath = "C:\\Program Files\\App\\MyApp.exe"   
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.FileName = "cmd.exe";
processInfo.WorkingDirectory = Path.GetDirectoryName(YourApplicationPath);
processInfo.Arguments = "/c START " + Path.GetFileName(YourApplicationPath);
Process.Start(processInfo);
Zviadi
  • 731
  • 2
  • 8
  • 19
10

Using Process.Start:

using System.Diagnostics;

class Program
{
    static void Main()
    {
        Process.Start("example.txt");
    }
}
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
4

How about you creat a batch file with the command you want, and call it with Process.Start

dir.bat content:

dir

then call:

Process.Start("dir.bat");

Will call the bat file and execute the dir

Carlo
  • 25,602
  • 32
  • 128
  • 176
  • Great idea! I did the Process.Start("test.bat") but an error pops up saying "An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll". Any ideas? – Jake Aug 10 '09 at 18:31
  • 1
    Oh nvm, fixed it. Thanks a lot Carlo. Really good idea, helped a lot. – Jake Aug 10 '09 at 18:34
3

You can use this to work cmd in C#:

ProcessStartInfo proStart = new ProcessStartInfo();
Process pro = new Process();
proStart.FileName = "cmd.exe";
proStart.WorkingDirectory = @"D:\...";
string arg = "/c your_argument";
proStart.Arguments = arg;
proStart.WindowStyle = ProcessWindowStyle.Hidden;
pro.StartInfo = pro;
pro.Start();

Don't forget to write /c before your argument !!

mfatihk
  • 136
  • 2
  • 11
2

Argh :D not the fastest

Process.Start("notepad C:\test.txt");
Acron
  • 1,378
  • 3
  • 20
  • 32
1

Are you asking how to bring up a command windows? If so, you can use the Process object ...

Process.Start("cmd");
JP Alioto
  • 44,864
  • 6
  • 88
  • 112
1

You can do like below:

var command = "Put your command here";
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.WorkingDirectory = @"C:\Program Files\IIS\Microsoft Web Deploy V3";
procStartInfo.CreateNoWindow = true; //whether you want to display the command window
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
label1.Text = result.ToString();
Mayank Tripathi
  • 809
  • 1
  • 7
  • 11
0

In addition to the answers above, you could use a small extension method:

public static class Extensions
{
   public static void Run(this string fileName, 
                          string workingDir=null, params string[] arguments)
    {
        using (var p = new Process())
        {
            var args = p.StartInfo;
            args.FileName = fileName;
            if (workingDir!=null) args.WorkingDirectory = workingDir;
            if (arguments != null && arguments.Any())
                args.Arguments = string.Join(" ", arguments).Trim();
            else if (fileName.ToLowerInvariant() == "explorer")
                args.Arguments = args.WorkingDirectory;
            p.Start();
        }
    }
}

and use it like so:

// open explorer window with given path
"Explorer".Run(path);   

// open a shell (remanins open)
"cmd".Run(path, "/K");
Matt
  • 25,467
  • 18
  • 120
  • 187