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

查看:320
本文介绍了通过.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:


copy / 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.

推荐答案

to cmd with / C ,有效地说 cmd / C copy / b t.wav ...

根据 cmd.exe /?使用

/ C< command>


执行$ b指定的命令$ b string然后终止

Carries out the command specified by string and then terminates

对于你的代码,它可能看起来像

For your code, it might look something like

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

注意


  • 测试您的命令是否可以工作的一个好方法是从命令提示符处实际尝试。如果您尝试执行 cmd.exe copy ... ,您会看到该副本不会出现。

  • 对可以作为参数传递的参数的长度的限制。从 MSDN :最大字符串长度是.NET Framework应用程序中的 2,003 字符和.NET Compact Framework应用程序中的 488 字符。

  • 您可以通过使用 System.IO 类打开文件并手动连接这些文件来绕过shelling命令。

  • 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天全站免登陆