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?

6
  • why not just set the working directory instead of using change directory command? Commented Dec 31, 2013 at 5:51
  • It must to change the directory to the selected path i.e. C:\Test because the file I am providing as an argument in the second command i.e. wsp.ddf is placed there. Commented Dec 31, 2013 at 5:53
  • 2
    right... so set the working directory to c:\test Commented Dec 31, 2013 at 5:54
  • You are hiding the command window. How do you know it isn't running as you are only changing directories? I would suggest not hiding it. Commented Dec 31, 2013 at 5:54
  • 1
    You need to use /c before your ProcessStartInfo argument. Commented Jul 20, 2017 at 7:24

6 Answers 6

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";
Sign up to request clarification or add additional context in comments.

3 Comments

I need to go to the path. C:\Test is selected from the folder browser dialog.
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?
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.
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.

1 Comment

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...
0

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

Comments

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";

Comments

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.

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.