简体   繁体   English

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

[英]Run .bat file from cmd.exe in C# with various arguments

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

and it works completely fine in cmd . 它在cmd完全可以正常工作。

this is my last code in C# which I really hoped would work, but however it doesn't: 这是我最后用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();
    }

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. 首先,在尝试执行我实际上必须要做的事情之后,我尝试将所有内容从cmd放入进程,但它没有用:将所有字符串值都作为参数,但它也不起作用。

Instead I am getting bunch of exceptions 相反,我遇到了一堆例外 第一张图片

第二张img

I'm literally out of options as I've sat all day doing this. 我整天都坐在那里,这使我毫无选择。 Does anyone have idea what problem it could be? 有谁知道这可能是什么问题?

UPDATE: 更新:

I've managed to run startInfo3 process. 我设法运行startInfo3进程。 But startInfo4 still doesn't work. 但是startInfo4仍然不起作用。 I've checked both lines seem to produce the same so what's wrong with it if they're are the same. 我已经检查了两行似乎产生相同的结果,所以如果它们相同则出了什么问题。 Do I pass them incorrectly? 我会错误地传递它们吗?

           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);

After digging a bit more I figured out an answer: 在深入研究之后,我找到了答案:

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);

Hope it helps someone! 希望它能对某人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM