How to pass arguments to command line utility using c#? - c#

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:
Is there a way to pass an argument to the command line utility using C#?

This is how this application is designed. It is meant to be interactive for new certificates. Please see the documentation with all of the allowed command-line arguments: https://www.win-acme.com/reference/cli

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

Calling Pyenv Python From C#

I'm trying to run Python from C# via a command line process.
System.Diagnostics.Process proc = new System.Diagnostics.Process {
StartInfo = new System.Diagnostics.ProcessStartInfo {
FileName = "/Users/username/.pyenv/shims/python",
Arguments = cmd+" "+args,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
I derived the FileName from using which:
username$ which python
/Users/username/.pyenv/shims/python
However, running python --version from the terminal, and then again through the C# process, yield different results:
username$ python -V
Python 2.7.11
And C#:
Python 2.7.10
I understand the base problem - that it's calling another version of Python, probably the base one that came from Apple. But what I don't understand is why, because as I understand it I'm telling it to call python from pyenv directly. Is there a way to get C# to use the same python executable I'm using from the terminal?
My bet is that the python shim depends on something else in your environment, and you should probably figure out what it’s eventually running, then call that directly.
According to the pyenv documentation, it's probably at:
$(pyenv root)/versions/2.7.11/bin/python
You might also be able to find out by running the shim with set -x, and looking at the command trace:
(set -x; python --version)

Getting CMD output via Process.Start with UseShellExecute = true in C#

I need to run an elevated command in my c# application and get the output.
I'm creating a new System.Diagnostics.Process instance, coupled with a new System.Diagnostics.ProcessStartInfo instance.
The command is being sent to cmd.exe, and unfortunately may require elevated user access (meaning UseShellExecute = true and Verb = "runas" must be present).
As a result of using UseShellExecute = true, RedirectStandardOutput will not work.
It's important I record the output of the command, but the only way I can see of getting this is adding something like > output.txt to the argument list, then calling System.IO.File.ReadAllText to read the result.
Can anyone think of less hacky way?

Start php.exe invisible with C#?

I want to start a php script with my local php.exe and don't want to see the cmd-window that appears when I do so.
This is my code:
System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory + #"php.exe", "script.php");
What argument can I use to make the php.exe invisible?
I read something about a "php-win.exe" but I have to do it with the normal php.exe and already tried some other methods (like hide.exe) but none of them would work.
Pass a Process start info to Process.Start instead.
var info = new ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory + #"php.exe", "script.php")
{
// check the various properties to define your process. For example:
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true
};
Process.Start(info);

Process.Start cmd results in "is not recognized"

When launching an application directly, the application is launched, but when launched through cmd - it's not.
For example:
Works:
Process.Start("firefox");
Doesn't work:
Process.Start(
new ProcessStartInfo
{
FileName = "cmd",
Arguments = "/k firefox"
});
I've tried setting UseShellExecute to true, but to no avail. I still get:
'firefox' is not recognized as an internal or external command,
operable program or batch file.
So, yes, I can specify the complete path. But is there a way to avoid that? Or in other words - what's the difference between the two that makes the second fail?
Haven't tested it but I guess you are probably looking for the start command:
Process.Start(
new ProcessStartInfo
{
FileName = "cmd",
Arguments = "/k start firefox"
});
As a tip, simply run "firefox" in a command prompt -> you'd get the same error message.

How to view Process.Start() Arguments as they are passed to executable

I'm trying to run a fortran executable with Process.Start and it is not working.
Process proc = new Process();
string args = "<C:\\file.in> C:\\file.out";
proc.StartInfo = new ProcessStartInfo(AppName, args);
proc.Start();
if I paste those arguments into a command window the application runs as expected. proc.Start() does not run as expected.
Any ideas how I can view what Start is actually passing as arguments? My gut feeling is that this is a quotes issue.
The executable launches and hangs, so I'm confident the AppName is getting passed in correctly, it looks like an argument problem.
I tried setting the WorkingDirectory to that of the input and output files as suggested in this question: process.start() arguments but that did not work.
Redirection with the < and > command line operators is a feature that's implemented by the command line processor. Which is cmd.exe. Use its /c argument to execute just a single command:
string args = "/c " + AppName + " < C:\\file.in > C:\\file.out";
proc.StartInfo = new ProcessStartInfo("cmd.exe", args);
proc.Start();
Your args string is exactly what is being passed as arguments to the executable. You can double check it reading your Process ProcessStartInfo.Arguments Property.
Something similar happened to me once, i.e., calling the executable from the command line worked and from code didn't, and it turned out that when called from the command line the executable was running on my PC's [C:] drive, and when called from code it was running on my PC's [E:] drive, which was full!
To check which directory your application is using to run the executable use the Directory.GetCurrentDirectory Method.

Categories

Resources