通过.NET运行CMD命令?命令、NET、CMD

2023-09-05 02:51:37 作者:暖神小阿哥

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的副本... ,你会看到副本不会发生。 是有限制的,你可以作为参数传递的参数的长度。从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.