Ldpetkov
Ldpetkov

Reputation: 33

Starting an .exe with two arguments from cmd in C# but arguments are not passed

I have the following piece of code:

       public static void startApplicationWithParameters()
        {
            string pathToProgram = "path to exe";
            string pathToLogFile= "path to log file";
            string pathToConfigFile = "path to xml file";
            string arguments = pathToLogFile + " " + pathToConfigFile;
            Console.WriteLine(arguments);

            Process startProcess = new Process();
            startProcess.StartInfo.FileName = pathToProgram;
            startProcess.StartInfo.Arguments = arguments;
            startProcess.Start();
           
        }

Printing the arguments shows the paths separated with space as expected. However, when running the method I get this message in the console:

"Expected two command line arguments, one indicating a path for a log file and the second to an XML configuration file Something went wrong when parsing command line options, stopping"

I'm trying to re-write a .bat file to c# that contains the following piece of code:

start /d %programPath% cmd.exe /k program.exe %programLogPath%\programLog.log %programConfigPath%\program.xml

The .bat file works as expected. What could be causing my error in the c# code? I read multiple threads about starting .exe file with c# but I couldn't find a solution.

I want the program to run in a separate console window and I don't want to wait for it to stop.

Upvotes: 0

Views: 151

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503839

My guess is that the paths have spaces in, and that your code received more than two parameters, instead of them not receiving the parameters at all. Try putting the paths in quotes:

string arguments = $"\"{pathToLogFile}\" \"{pathToConfigFile}\"";

Or better, use ArgumentList instead of Arguments, as then you don't need any quoting or even putting in the spaces.

Upvotes: 3

Related Questions