通过.NET运行CMD命令? [英] Running cmd commands via .NET?

查看:162
本文介绍了通过.NET运行CMD命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Diagnostics.Process proc0 = new System.Diagnostics.Process();
proc0.StartInfo.FileName = "cmd";
proc0.StartInfo.WorkingDirectory = Path.Combine(curpath, "snd");
proc0.StartInfo.Arguments = omgwut;

和现在的一些背景......

And now for some background...

string curpath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

omgwut是这样的:

omgwut is something like this:

拷贝/ B a.wav + b.wav + ... + y.wav + z.wav output.wav

copy /b a.wav + b.wav + ... + y.wav + z.wav output.wav

和没有任何反应都没有。所以,很显然什么是错的。我也试图复制的可执行文件,但不起作用。

And nothing happens at all. So obviously something's wrong. I also tried "copy" as the executable, but that doesn't work.

推荐答案

尝试prefixing你的论点到CMD与 / C ,有效地称 CMD / C复制/ B t.wav ...

CMD.EXE /?

/℃下命令>

执行由指定的命令   字符串,然后终止

Carries out the command specified by string and then terminates

有关您的code,它可能看起来像

For your code, it might look something like

// .. 
proc0.StartInfo.Arguments = "/C " + omgwut;

注:

  • 要测试你的命令是否是去工作是真正从命令提示符尝试的一个好办法。如果你试着做 cmd.exe的副本... ,你会看到副本不会发生。
  • 是有限制的,你可以作为参数传递的参数的长度。从<一个href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments.aspx">MSDN: 最大字符串长度 2003 在.NET Framework应用程序的字符和 488 字符在.NET Compact Framework应用程序。
  • 您可以使用 System.IO 类来打开这些文件并手动将它们连接起来绕过炮轰出命令。
  • A good way to test whether your command is going to work is to actually try it from a command prompt. If you try to do cmd.exe copy ... you'll see that the copy doesn't occur.
  • There are limits to the length of the arguments you can pass as arguments. From MSDN: "The maximum string length is 2,003 characters in .NET Framework applications and 488 characters in .NET Compact Framework applications."
  • You can bypass the shelling out to command by using the System.IO classes to open the files and manually concatenate them.

这篇关于通过.NET运行CMD命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆