Start process with IO stream redirection - c#

How to start process and run command like this:
mysql -u root --password="some-password" < "some-file.sql"
Is it possible to do with process.Start()?
I need cross-platform solution (we cannot use cmd.exe).

Yes, this is possible through the System.Diagnostics.Process class. You need to set RedirectStandardInput to true, after which you can write the content of a file redirect the standard input of a process, and write the contents of the file to the Process.StandardInput (which is a StreamWriter)
This should get you started:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "mysql.exe", // assumes mysql.exe is in PATH
Arguments = "-u root --password=\"some-password\"",
RedirectStandardInput = true,
UseShellExecute = false
},
};
process.Start();
process.StandardInput.Write(File.ReadAllText("some-file.sql"));
Update: this is pretty well documented [here](
https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardinput)

Related

How to write a command to cmd.exe when using ProcessStartInfo

I have some script files that would usually be edited and run through a ui, but the offer to run on the command line when using the syntax: program.exe scriptfile.fmw --userparameter "some parameter".
If I simply write
arguments = "program.exe scriptfile.fmw --userparameter \"some parameter\"";
Process process = Process.Start("cmd.exe", $"/K {arguments};
process.WaitForExit();
the commandline starts, calls the correct exe and runs the script.
Now I want to retrieve the response of the script after it ran to catch possible error messages.
As far as I can see this requires to use ProcessStartInfo and that's basically where my problem seems to start.
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "cmd.exe",
Arguments = $"/K {arguments}",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process process = Process.Start(startInfo);
process.BeginErrorReadLine();
process.WaitForExit();
The code above opens a commandline window, but never uses the arguments given.
It escapes me on how I should hand the argument line over to the cmd.exe - process.
I was playing around with RedirectStandardInput and its StreamWriter, but never managed to get anything written into the cmd-window.
The same goes for the RedirectStandardOutput, where I would like to gather the script response in the cmd-window as a string or string[], to parse for specific exit codes of the script.
I think this is what you want, have a look below :
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "program.exe", //You must use your program directly without invoking through cmd.exe
Arguments = "scriptfile.fmw --userparameter \"some parameter\"", //Add parameters and arguments here needed by your application
UseShellExecute = false,
EnableRaisingEvents = true, //You are missing this
RedirectStandardOutput = true,
RedirectStandardError = true
};
process.ErrorDataReceived += process_ErrorDataReceived; //You should listen to its output error data by subscribing to this event
process.BeginErrorReadLine();
process.Start(startInfo);
process.WaitForExit(); // You may now avoid this
Then at here do anything with your received error data!
private static void process_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
DoSomething(e.Data); // Handle your error data here
}
EDIT-(1) : Please try this solution and ping me if it works or if you need some extra help. Everyone in comments is suggesting that you must not use cmd.exe to invoke your program as it may causes debugging overhead, performance issue and you might not get error details as well.
Cheers!

Issue WSL commands and read returned value from C#

I am writing a code to execute some commands against wsl, parsing and reading the returned value is important.
Project is a .net core console app 3.1
wsl2 is enabled on the system
for example, listing all the available wsl images on my local machine i am using a snippet found in an answer provided in another "kind of related" SO post.
using (var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"cmd.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true,
}
})
{
proc.Start();
proc.StandardInput.WriteLine("wsl --list");
System.Threading.Thread.Sleep(500);
proc.StandardInput.Flush();
proc.StandardInput.Close();
proc.WaitForExit(5000);
var c = proc.StandardOutput.ReadToEnd();
Console.WriteLine(c);
Console.ReadLine();
}
now the expected output should be
what i am getting is
if i inspect using breakpoint i get this in "var c"
Ideally i want to be able to have a list that contains the 2 dockers items inside C#, changing the wait time didn't help.
in the ProcessStartInfo you have to set
StandardOutputEncoding = Encoding.Unicode;
StandardErrorEncoding = Encoding.Unicode;
for direct call of wsl use additionally:
FileName = #"wsl.exe";
Arguments = #"-l -v";

C# Read text (output?) from console that runs as a Process

I'm trying to check if username and password for git repository is valid. In console I run:
git clone http://username:password#server/test.git
And I get:
fatal: Authentication failed for ...
So now I know username and password are not valid. I'm trying to run this command as a process:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "git.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
WorkingDirectory = "some_directory"
CreateNoWindow = true,
Arguments = "git clone http://username:password#server/test.git"
},
};
process.Start();
I'd like to access the result of this command. Both process.StandardError and process.StandardOutput are equals string.Empty. Is there any way to read the result?
Normally you should read the exit code of the process.
process.ExitCode
If the process failed, the return value should be non 0. Of course you can only retrieve the exit code after the process completes.
So:
if (process.ExitCode != 0)
//error
Please note: I haven't tested it but it is standard convention.
To read the output, one normally uses:
string output = process.StandardOutput.ReadToEnd();
string err = process.StandardError.ReadToEnd();
Console.WriteLine(output);
Console.WriteLine(err);
What you really need is your standard output, which can be accessed as below:
string stdout = p.StandardOutput.ReadToEnd();
and use p.WaitForExit(); right after because sometimes it takes a while to give error message.

Process.Close() is not working and Process.CloseMainWindow() is throwing System.InvalidOperationException

I am trying to open an excel file by using the Process.Start() and then after some time I have to close the opened excel file. Here is the code for this.
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = file
};
process.Start();
Thread.Sleep(60000);
process.CloseMainWindow();
I am able to open the file but at process.CloseMainWindow(), it throws an exception "No process is associated with this object". I have tried process.Close() but it didn't worked too(no exception was thrown). The process object shows System.InvalidOperationException. Please anyone help me in resolving this issue. Thanks in advance
Assuming that you want your code to continue after the user is done manipulating the file you want excel to open, I would suggest that you use process.WaitForExit();
You can then set a timeout on the wait method like this:
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = file
};
process.Start();
process.WaitForExit(60000);
If the timeout is reached the process will stop and out can then parse the output for errors by adding:
RedirectStandardError = true
RedirectStandardOutput = true
to your StartInfo() object

Cmd not elevating when redirecting input/output

I'm trying to run automated installations via CMD commands. The programs output progress and I need to capture that output and calculate total progress in a nice window. In my understanding it is impossible to elevate and redirect at the same time. I've tried...
Running cmd elevated and feeding it commands.
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Verb = "runas",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
proc.StandardInput.WriteLine("command");
Running cmd with command as an argument
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C " + "command",
Verb = "runas",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
To no effect. Is there any tricks to this? Elevating after input/output has been captured? I need this to work. Would this be possible with psexec?
If you own the installer programs, you can rewrite them so that they do not use standard streams as a means of communication, but for example named pipes. If you don't, you can write a wrapper program that runs elevated (you run it with UseShellExecute), does not use the standard streams, but runs the installer programs and redirects their input (you run them without UseShellExecute) reporting progress to the nonelevated main program via named pipes or some other means. I hope this diagram makes it clearer:
non-elevated.exe
\
\ named pipes
\
elevated-wrapper.exe (ran with UseShellExecute=true)
\
\ redirected standard streams
\
installer-program.exe (ran with UseShellExecute=false and redirected streams)

Categories