来自 C# 的多个 CMD 命令?
所以我想知道,如何使用 C# 在 CMD 中执行多个命令?我的意思是……我有一个 .exe 文件,它依赖于通过 cmd 变量 (VAMP_PATH) 查找文件 [是的,我正在使用 VAMP 插件]。所以我在 CMD 中使用它的方式是:
-set VAMP_PATH:C:\ (press Enter)
-sonic-annotator.exe -d etc...
但是,我在尝试将 CMD 与 C# 一起使用方面还相当新,所以我想知道我应该做什么?目前我有这样的代码:
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 命令没有问题。
我的问题是我不知道如何添加另一个命令。我是否只需要复制 p.StandardInput.WriteLine 命令并输入另一个命令作为参数?因为我读到这方面存在一些问题。
另外,我想问一下,因为如果没有 p.StandardInput.WriteLine 命令,并且仅使用 ProcessStartInfo 中的“args”参数,我的命令根本不会执行(即使将 \c 添加到 args)。你认为这是为什么?
谢谢!
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...
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();
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.
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.
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?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不先让第一个进程完成,则无法从一个进程对象运行两个命令(实际上是两个进程)。
运行第一个,调用
p.WaitForExit();
,然后构建并运行第二个。为了启动该过程,您不需要像当前那样将参数写入命令行。它应该按照你正在做的方式工作。
可以为您设置参数。
You can't run two commands (effectively two processes) from one process object, without first letting the first process complete.
Run the first one, call
p.WaitForExit();
and then build and run the second.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.
May work for you for setting up the Arguments.