C# Process Argument is not working with startInfo.Arguments

188 Views Asked by At

I am doing an unattended installer, I ran it with cmd as below, and it is working without any problem.

setup.exe -q -J -Djava.security.manager=allow

Now I am trying to use the same arguments in my c# console application code, but it will ignore the arguments. Can you please check the method below and support if there is a way to do it?

static int RunSetup(out string stdout)
{
    try
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;                
        startInfo.Arguments = "-q -J -Djava.security.manager=allow";
        startInfo.FileName = "setup.exe";
        startInfo.WorkingDirectory = Environment.CurrentDirectory;

        process.StartInfo = startInfo;
        process.Start();
        stdout = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        int exitCode = process.ExitCode;

        process.Close();
        return exitCode;
    }
    catch (Exception exception)
    {
        throw new Exception("exeption = " + exception.Message);
    }
}
1

There are 1 best solutions below

5
Per-Frode Pedersen On

I wrote a small .net console application that just prints the arguments that are passed to the application:

Console.WriteLine(string.Join(":", args ));

and named it setup.exe.

When i used your code example to call it it prints out -q:-J:-Djava.security.manager=allow as expected, so your code seem to work just as you expected. This was tested using .NET 7, though.