来自C#的多个CMD命令? [英] Multiple CMD Commands from C#?

查看:51
本文介绍了来自C#的多个CMD命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想知道,如何使用C#在CMD中执行多个命令?我的意思是……我有一个.exe文件,该文件依赖于通过cmd变量(VAMP_PATH)查找文件[是的,我正在使用VAMP插件].所以我在CMD中使用的方式是:

So I'm wondering, how do I execute multiple commands in CMD using C#? What I mean is this ... I have a .exe file that relies on finding the files via a cmd variable (VAMP_PATH) [yes I am using VAMP Plugin]. So the way I use this in CMD would be:

-set VAMP_PATH:C:\ (press Enter)
-sonic-annotator.exe -d etc...

但是,我在尝试将CMD与C#结合使用时还很陌生,所以我想知道应该怎么做?目前,我有以下代码:

However, I am fairly new in trying to use CMD with C# so I am wondering what I should do? Currently I have this code:

Process p = new Process();
string args = "\"" + sonicannotatorpath + "\" -t \"" + transpath + "\" \"" + filepath + "\" -w csv --csv-force";
p.StartInfo = new ProcessStartInfo("cmd", args)
{
   RedirectStandardOutput = true,
   RedirectStandardInput = true,
   UseShellExecute = false,
   CreateNoWindow = true
};
p.Start();
p.StandardInput.WriteLine(args);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine("DONE");
Console.Read(); 

上面的代码仅执行我的CMD命令的第二行...但是由于.exe所需的文件已经位于其默认位置,因此不指定VAMP_PATH命令就没有问题.

The code above only performs the second line from my CMD command ... but since the files the .exe need is already in its default location, there is no problem not specifying the VAMP_PATH command.

我的问题是我不确定如何添加另一个命令.我是否只需要复制p.StandardInput.WriteLine命令并仅输入另一个命令作为参数?因为我读到有一些问题.

My problem is I am not sure how to add another command. Would I simply just need to copy the p.StandardInput.WriteLine command and just input another command as a parameter? Because I read that there are some problems regarding this.

此外,我想问一下,因为没有p.StandardInput.WriteLine命令,并且仅带有ProcessStartInfo中的'args'参数,我的命令根本不执行(即使将\ c添加到args中).你为什么这么认为?

Additionally, I would like to ask because without the p.StandardInput.WriteLine command, and just with the 'args' parameter in ProcessStartInfo, my command does not execute at all (even with the \c added to args). Why do you think this is?

谢谢!

推荐答案

如果不先完成第一个进程,就不能从一个进程对象运行两个命令(实际上是两个进程).

You can't run two commands (effectively two processes) from one process object, without first letting the first process complete.

运行第一个,调用 p.WaitForExit(); ,然后构建并运行第二个.

Run the first one, call p.WaitForExit(); and then build and run the second.

对于启动该过程,您不需要按照当前的方式将Arguments写入命令行.它应该按照您的方式工作.

For starting the process you shouldn't need to write the Arguments to the command line the way you currently are doing it. It should work the way that you are doing it.

p.StartInfo.Arguments = args;
p.StartInfo.FileName = "cmd";

可以为您设置参数.

这篇关于来自C#的多个CMD命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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