1

I want to execute cmd commands from Visual Studio (using c#). There are two commands which I want to run.

I referred this post, but not working in my case.

My code is:

private void ExecuteCmdCommands()
{
    string strCmdText;
    strCmdText = @"cd C:\Test + makecab /f wsp.ddf";

    Process process = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = strCmdText;
    process.StartInfo = startInfo;
    process.Start();
}

When I run this code, only command prompt is open no commands are executed. What am I missing?

Community
  • 1
  • 1
Mohemmad K
  • 809
  • 7
  • 33
  • 74

6 Answers6

2

Don change directory, simply shell open the file:

strCmdText = @"C:\Test + makecab /f wsp.ddf";

Edit: Set the working directory:

startInfo.WorkingDirectory = @"C:\Test";
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I need to go to the path. `C:\Test` is selected from the folder browser dialog. – Mohemmad K Dec 31 '13 at 05:54
  • Thank you sir for replying, I set the `Working Directory` and commented the line which hides the command window. After running the program I found the command window is opened with current working directory to `C:\Test` but the `makecab` command did not execute. What should I do? – Mohemmad K Dec 31 '13 at 06:00
  • Once you set the working directory, you won't need the 'cd' anymore. You still need to use the '/c' before your command, to tell cmd that the following text is a command it should execute. – Stephan Zaria Dec 31 '13 at 06:08
2

In order to call 2 command from one line, you need to use the & sign.

In your case: @"/c cd C:\Test & makecab /f wsp.ddf";

Also dont forget the /c flag, telling the cmd to execute the command.

Stephan Zaria
  • 496
  • 5
  • 12
  • Thank you sir for replying, the command for changing the directory did not work, to do that, I set the `WorkingDirecotry` property and after that the command `makecab` worked preceding `/c` flag... – Mohemmad K Dec 31 '13 at 06:07
0

try to change to strCmdText = @"C:\Test + makecab /f wsp.ddf";

Yanshof
  • 9,659
  • 21
  • 95
  • 195
0

Just a guess, but it looks like you are attempting to execute 2 different command on the same line??

First changing the directory is not necessary, and you don't need to execute cmd.exe. Just create a process directly for the makecab program.

startInfo.Filename = @"makecab.exe";
startInfo.Argumanets = @"/f c:\test\wsp.ddf";
NebulaSleuth
  • 833
  • 1
  • 7
  • 18
0

The issue here is that the commands you're passing in are arguments and not commands (which would have to be piped in through the StandardInput pipe). Fortunately, you can use the "/c" argument as mentioned here. I'm not sure if it will work with the "+" operator.

Note, as someone else mentioned, you should also set the working directory using the available property or your program will fail if not run with a "C:" working directory.

Community
  • 1
  • 1
N Jones
  • 1,004
  • 11
  • 18
0
you have to do run shell commands from C#

string strCmdText; 
strCmdText= "/C copy /b Image1.jpg + xyz.rar Image2.jpg"; System.Diagnostics.Process.Start("CMD.exe",strCmdText);

**EDIT:**

This is to hide the cmd window.

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b Image1.jpg + xyz.rar Image2.jpg";
process.StartInfo = startInfo;
process.Start();
Tejas
  • 467
  • 5
  • 13