0

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);
    }
}
Florian
  • 1,019
  • 6
  • 22
w3000cpu
  • 13
  • 1
  • 7
  • everything looks ok. Check if `Environment.CurrentDirectory` is really directory which contains `setup.exe` or try with full path `FIleName`, such as `startInfo.FileName = "c:\\dev\\mysetup\\setup.exe` – Nino Jan 03 '23 at 10:43
  • Yes, it is correct because it will run the exe without the arguments – w3000cpu Jan 03 '23 at 11:14
  • Also, I am able to do startInfo.Arguments = "-q" only if I add more arguments it will ignore. – w3000cpu Jan 03 '23 at 11:25

1 Answers1

3

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.

Per-Frode Pedersen
  • 1,027
  • 1
  • 6
  • 15
  • OP's code works on .net 6 (I used some different `FileName` and arguments were passed correctly). The only thing that I think could cause problem is `WorkingDirectory` property – Nino Jan 03 '23 at 10:52
  • No, it is correct because it will run the exe without the arguments, Also I am not getting the "File not found " exception. – w3000cpu Jan 03 '23 at 11:23
  • Also, I am able to do startInfo.Arguments = "-q" only if I add more arguments it will ignore. – w3000cpu Jan 03 '23 at 11:25
  • @w3000cpu have you tried to add arguments to `ArgumentList` instead of setting `Arguments` property? Like this: `startInfo.ArgumentList.Add("-q"); startInfo.ArgumentList.Add("-J");` – Nino Jan 03 '23 at 11:39
  • Yes I just tried now and same issue. – w3000cpu Jan 03 '23 at 12:38