Click here to Skip to main content
14,147,016 members
Rate this:
 
Please Sign up or sign in to vote.
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.

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


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?
Posted
Updated 20-Jun-18 11:49am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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", 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.
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190518.1 | Last Updated 20 Jun 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100