0

Worst topic but can't really say much more.

Thing is, i am trying to run a certain command from cmd, if i do this normally in windows, it's flawless, in C# it doesn't work, even though it's the exact same string.

Here is how i do it:

        Process cwebp = new Process();
        cwebp.StartInfo.FileName=("cmd.exe");
       cwebp.StartInfo.Arguments = Settings.EncoderSettings[0];
       cwebp.Start();

And well arguments is pretty much anything, for example:

opusenc --bitrate 100 input.wav output.opus

Is there any fundamental issue here? Been searching a lot and can't find any information, everything says (use Arguments), and i do, and it doesn't work as expected.

3
  • What does "it doesn't work as expected" mean? Commented Jul 19, 2014 at 16:58
  • It doesn't work at all. CMD opens at the "build program" location. that's it, no execution, nothing. Commented Jul 19, 2014 at 17:01
  • Try executing the equivalent command and see what happens cmd opusenc --bitrate 100 input.wav output.opus vs cmd /C opusenc --bitrate 100 input.wav output.opus. For more info, see the parameters for CMD.exe. Commented Jul 19, 2014 at 17:21

2 Answers 2

5

Additionally to Steve's answer, you can start your command directly, without using cmd in the first place:

Process.Start("opusenc", "--bitrate 100 input.wav output.opus");
Sign up to request clarification or add additional context in comments.

Comments

2

To execute a shell command you need to add the parameter /C (/K) on the arguments line

 Process cwebp = new Process();
 cwebp.StartInfo.FileName=("cmd.exe");
 cwebp.StartInfo.Arguments = "/C " + Settings.EncoderSettings[0];
 cwebp.Start();

Without it, the Process.Start method starts the cmd command processor, but, this one, exits immediately without processing the passed arguments.

5 Comments

This makes it do what i expect. Is it possible to use the same process to run more commands after it's done with the first one, or do i have to remake it with startinfo arguments?
You can run a batch file with your commands or use the command concatenation separator && Type cmd.exe /? to get the options available
Running a batch file is by far the easiest way; but it is also possible to make the cmd read its input from a stream you provide by redirecting its StdIn.
@Steve I see, thing is i am doing three processes, one of them combines 2 (uses stdin to another), i can run this in a Batch fine, but doing a batch for C#, i guess there is a way to change variables in it+
Batch files are simple text files, it is relatively easy to build them directly in your code and then execute them. Or you could check if the command redirection works for you (the pipe operator for example)

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.