Home > Back-end >  How to pass arguments to command line utility using c#?
How to pass arguments to command line utility using c#?

Time:06-22

I'm writing an application, and at one point it launches win-acme and needs to pass some parametres to it. I'm successfully opening powershell and launching win-acme, but it doesn't pass arguments to it. So, I have this code:

Process wacsProcess = Process.Start(new ProcessStartInfo
    {
        FileName = @"C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe",
        Arguments = (@"cd C:\inetpub\letsencrypt ; .\wacs.exe ; N"),
        RedirectStandardOutput = true,
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Hidden
   });

File.WriteAllText(".\\OutPutAfterFirstLaunch.txt", 
    wacsProcess.StandardOutput.ReadToEnd());

It opens command-line utility, but doesn't give it the last parametr "N". I guess that is because I'm passing this parametr to the powershell, but it's still working with win-acme. It looks like this:

enter image description here

Is there a way to pass an argument to the command line utility using C#?

CodePudding user response:

Have you tried just calling wacs.exe directly? It is a standalone exe and should not require PowerShell to run.

Process wacsProcess = Process.Start(new ProcessStartInfo
    {
        FileName = @"C:\inetpub\letsencrypt\wacs.exe",
        Arguments = ("N"),
        RedirectStandardOutput = true,
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Hidden
   });

File.WriteAllText(".\\OutPutAfterFirstLaunch.txt", 
    wacsProcess.StandardOutput.ReadToEnd());

CodePudding user response:

Is there a particular reason that you must launch the process from powershell?

You should be able to read the stdout of the process if you launch it directly the same way as if you were reading the output from your powershell window (the output powershell displays is just the stdout of the process anyways.)

You can also try passing the N parameter with the executable,

Arguments = (@"cd C:\inetpub\letsencrypt ; .\wacs.exe N;"),
  • Related