问题描述
我一直在努力开始运行以下代码的流程:
I've been trying all day to start process which would run the following code:
C:\bin\ant.bat -f=C:\build.xml -DinputFile=C:\Desktop\Book1.xml -DstartDate=2018-06-20 -DxslFile=ProcessingDate -DoutputFile=fff
并且它在cmd中完全正常。
我尝试过:
这是我在C#中的最后一个代码,我真的希望它可以工作,但不是这样:
and it works completely fine in cmd.
What I have tried:
this is my last code in C# which I really hoped would work, but however it doesn't:
public void run() {
string antFile = @"C:\ant.bat";
string build = @"C:\build.xml";
string inputFile = @"C:\Book1.xml";
string startDate = "2018-05-23";
string outputFile = "ff";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe", "/c" + @"C:bin\ant.bat -f=C:\build.xml -DinputFile=C:\Desktop\Book1.xml -DstartDate=2018-06-20 -DxslFile=ProcessingDate -DoutputFile=test0.xsl");
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
ProcessStartInfo procStartInfo2 = new ProcessStartInfo("cmd.exe", "/c" + antFile + "-f=" + build + "-DinputFile=" + inputFile + "-DstartDate=" + startDate + "-DxslFile=" + startDate + "-DoutputFile=" + outputFile);
Process proc2 = new Process();
proc2.StartInfo = procStartInfo2;
proc2.Start();
}
首先,我试图将cmd中的所有内容都放到流程中但是它不起作用,在我尝试做我真正需要做的事情之后:将所有字符串值作为参数但它也不起作用。
相反,我得到一堆例外。我添加了这些例外截图的链接: []
因为我已经完全没有选择了整天坐着这样做。有没有人知道它可能是什么问题?
Firstly, I've tried to just put everything from cmd to the process but it didn't work, after I tried to do what I actually have to: put all the string values as arguments but it didn't work either.
Instead I am getting bunch of exceptions. I add a link to screenshot of these exceptions: Screenshot 19 — imgbb.com[^] [^]
I'm literally out of options as I've sat all day doing this. Does anyone have idea what problem it could be?
推荐答案
cmd.exe /cC:\ant.bat-f=C:\build.xml-DinputFile=C:\Book1.xml-DstartDate=2018-05-23-dXslFile=2018-05-23-DoutputFile=ff
只是连接字符串一起不会自动将空格放在适当的位置。您必须自己解释。
此外,如果您将字符串放入变量而不是直接将它们组合为函数调用中的参数,它将极大地帮助调试:
Just concatenating strings together does not automatically put spaces in the appropriate places for you. You have to account for that yourself.
Also, it greatly helps debugging if you put the strings into variables instead of directly assembling them as an argument in the function call:
string arguments=
了解如何使用调试器!这可能很容易找到调试器和鼠标悬停在变量上以查看其内容。
调试器可以调试你和你了解代码及其工作原理。
Learn how to use the debugger! This could have been EASILY found with just the debugger and a simple hover of the mouse over a variable to see its contents.
The debugger is there to debug YOU and you're understanding of the code and how it works.
public static void RunCmd(string executeString)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(executeString);
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
if (process.ExitCode == -1)
throw new Exception(process.StandardOutput.ReadToEnd());
}
这篇关于使用各种参数从C#中的cmd.exe运行.bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!