Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用各种参数从C#中的cmd.exe运行.bat文件_C#_.net_Batch File_Cmd_Process - Fatal编程技术网

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

使用各种参数从C#中的cmd.exe运行.bat文件,c#,.net,batch-file,cmd,process,C#,.net,Batch File,Cmd,Process,我一整天都在尝试启动进程,该进程将运行以下代码: C:\bin\ant.bat-f=C:\build.xml-DinputFile=C:\Desktop\Book1.xml-dstardate=2018-06-20-DxslFile=ProcessingDate-DoutputFile=fff 而且它在cmd中完全可以正常工作 这是我在C#中的最后一段代码,我真的希望它能工作,但是它没有: public void run() { string antFile = @"C:\ant

我一整天都在尝试启动进程,该进程将运行以下代码:

C:\bin\ant.bat-f=C:\build.xml-DinputFile=C:\Desktop\Book1.xml-dstardate=2018-06-20-DxslFile=ProcessingDate-DoutputFile=fff

而且它在
cmd
中完全可以正常工作

这是我在C#中的最后一段代码,我真的希望它能工作,但是它没有:

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
到进程的所有内容都放进去,但都不起作用,在我试着做我必须做的事情之后:把所有字符串值都放进参数中,但也不起作用

相反,我得到了一堆例外

我几乎没有选择了,因为我已经坐了一整天了。有人知道这可能是什么问题吗

更新:

我已经设法运行了
startInfo3
进程。但是
startInfo4
仍然不起作用。我已经检查过两条线似乎产生了相同的结果,如果它们是相同的,那么有什么问题呢。我传错了吗

           ProcessStartInfo startInfo3 = new ProcessStartInfo();
            startInfo3.FileName = "cmd.exe";
            startInfo3.Arguments = "/c" + @"C:\ant.bat -f=C:\build.xml -DinputFile=C:\Book1.xml -DstartDate=2018-06-20 -DxslFile=ProcessingDate -DoutputFile=fff";
            Process.Start(startInfo3);

            ProcessStartInfo startInfo4 = new ProcessStartInfo();
            startInfo4.FileName = "cmd.exe";
            startInfo4.Arguments = "/c" + antFile + "-f=" + build + "-DinputFile=" + inputFile + "-DstartDate=" + startDate + "-DxslFile=" + startDate + "-DoutputFile=" + outputFile;
            Process.Start(startInfo4);

在进一步挖掘之后,我找到了一个答案:

ProcessStartInfo procStartInfo5 = new ProcessStartInfo();
            procStartInfo5.FileName = "cmd.exe";
            procStartInfo5.Arguments = $"/c{antFile} -f={build} -DinputFile={inputFile} -DstartDate={startDate} -DxslFile=ProcessingDate -DoutputFile={outputFile}";
            Process.Start(procStartInfo5);

希望它能帮助别人

您的代码看起来不错,只是有点冗长。也许你的问题与环境有关?如何运行C#程序,如何手动运行
cmd
,是否可以尝试一个简单的控制台测试程序或获取LINQPad并尝试一下?看起来您的命令字符串中没有任何空格(对于proc2)。这就是命令行解析器用来分离传递的参数的方法。我已经更新了问题,您能看一下吗?