I want to get output text from the external process that runs the application and this application displays something that looks like a console dialog box like in the screenshot below:
I tried to get output via StandardOutput:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = pathToExeFile,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
},
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
var err = process.StandardError.ReadToEnd();
process.WaitForExit();
But in this case both output and err variables have null values only. I assume that this is because the output from the app is not available via stdout but from something which looks like a dialogbox/messagebox. Is there any way to get this output?
Related
So i have two applications.
In the console app let's say i do Console.WriteLine("Hello World");
I then compile to an exe.
From my main forms application i want to call this application
var processStartInfo = new ProcessStartInfo
{
FileName = #"C:\ConsoleApp1.exe",
Arguments = "Arguments",
RedirectStandardOutput = true,
UseShellExecute = false
};
var process = Process.Start(processStartInfo);
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
However, it does not read the output. Is there another way i should output from the console app rather than Console.WriteLine("Hello World");
The problem with the code is that you are reading the output before the command finishes. Change the sequence where you are reading the result after completion
var process = Process.Start(processStartInfo);
process.WaitForExit();
var output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output)
I'm starting a Python process using the C# Process class. The standard output is redirected as I want to capture it. I'm able to get the standard output properly except when it crashes. We are aware that it will crash. We need to inspect the standard output when it crashes. We are not getting exact stdout once crashed.
When I run that script in the console manually, I can see the exact output at the time of the crash, but when I redirect the stdout using the > operator, the output is not written to the file, if the process crashed. I suspect that the I/O buffers are not getting flushed, if the process crashes. Is this true? Thanks in advance.
var lastLineOfStdOut = null;
var p = new Process
{
StartInfo =
{
FileName = ... ,
Arguments = ... ,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
p.OutputDataReceived += (sender, e) =>
{
if (e.Data == null) return;
lastLineOfStdOut = e.Data;
Console.WriteLine(e.Data);
};
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
var exitCode = p.ExitCode; // Use this to figure out if it crashed
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)
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.
When I redirect stdout, the new script's process window does not show all the printing (obviously, as it's redirected...).
Is there a way to both show the printings in the script window (be it PowerShell or Python) and also capture its output after the process ends?
var process = new Process {
StartInfo = {
FileName = path,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true, // I want to capture stdout
}
};
using (process) {
process.Start();
process.WaitForExit();
var stdout = await process.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
// I do have the output in stdout now, but the script's window hasn't shown anything while it was running.
// It makes me sad :-(
}