简体   繁体   中英

Running Commands from C#

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.

除了史蒂夫的答案,您可以直接启动命令,而无需首先使用cmd

Process.Start("opusenc", "--bitrate 100 input.wav output.opus");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM