使用各种参数从C#中的cmd.exe运行.bat文件

使用各种参数从C#中的cmd.exe运行.bat文件

问题描述:

我一直在努力开始运行以下代码的流程:



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中的所有内容都放到流程中但是它不起作用,在我尝试做我真正需要做的事情之后:将所有字符串值作为参数但它也不起作用。



相反,我得到一堆例外。我添加了这些例外截图的链接:截图19 - imgbb.com [^] [ ^ ]



因为我已经完全没有选择了整天坐着这样做。有没有人知道它可能是什么问题?



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?

你没有注意命令行参数之间的空格。



您的汇编命令行如下所示:

You're not paying attention to the spaces in between your command line arguments.

Your assembled command line looks like this:
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=


/ c {antFile} -f = {build} -DinputFile = {inputFile} -DstartDate = {startDate} -DxslFile = {startDate} -DoutputFile = {} OUTPUTFILE跨度>;
ProcessStartInfo procStartInfo2 = new ProcessStartInfo( cmd .exe,参数);
"/c {antFile} -f={build} -DinputFile={inputFile} -DstartDate={startDate} -DxslFile={startDate} -DoutputFile={outputFile}"; ProcessStartInfo procStartInfo2 = new ProcessStartInfo("cmd.exe", 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.


您还应该创建一个执行该过程的方法。这样你就可以适当地包装它,而不是重复代码。以下是我发现并略微修改的一些代码:

You really should also be creating a method to execute the process. That way you can wrap it appropriately, and not duplicate code. Here is some code I found and slightly modified:
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());
  }