简体   繁体   English

从C#运行命令

[英]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. 问题是,我正在尝试从cmd运行某个命令,如果我在Windows中正常执行此命令,它是完美无缺的,在C#中,即使它是完全相同的字符串,也无法正常工作。

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 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. 一直在搜索很多东西,找不到任何信息,所有内容都说(使用Arguments),我也这样做了,而且它没有按预期工作。

除了史蒂夫的答案,您可以直接启动命令,而无需首先使用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 要执行shell命令,您需要在参数行中添加参数/ C(/ K)

 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. 没有它,Process.Start方法将启动cmd命令处理器,但是这一步将立即退出,而不处理传递的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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