In the main method I want to start a process which is to run some test cases, and when the process returns I want to know how many test cases failed. In this case, I want the process to be able to return a value to the main process after it is done executing. Is there a way to do this? My code is like below:
ProcessStartInfo processInfo = new ProcessStartInfo(exeFilePath, Parser.Default.FormatCommandLine(options));
var p = Process.Start(processInfo);
// need to get the number of failed test cases from the process
Any help is appreciated.
Related
I use the following code to call git from C#:
var pInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = "checkout master",
UseShellExecute = false
};
using var p = Process.Start(pInfo);
Console.WriteLine(p.Id);
p.WaitForExit();
The process ID printed is, lets say 3709. When my program ends, I look at the Task Manager and I see a git.exe process still running with a different ID, say 8865. This process remains running indefinitely.
It seems to me that the git process started by my program spawns a second git process that never exits. Is there any way for me to prevent this behavior? As it is, every time I run my program, there is yet another git.exe process left behind indefinitely, adding up to a lot over time, until I reboot.
(Alternatively, is there any other way to invoke git other than by running the executable?)
I often find myself using different commands on pc's to check stuff in the system and I wanted to basically create a small standalone exe that will execute all my common commands and give me the output of each of them without me entering them manually...
I want to note that I know this isn't the first question on google on the subject of executing a cmd command with c# but non fit my command requirements, for example many of them execute commands such as copy or move or make and non of them have a complex output, I want to execute for example the "sfc /scannow" command, which outputs a progress bar which measures the progress of course and a final output, the issue with that is I have tried many ways to attempt that but all failed, a shell execution works very well with opening another cmd windows and even requiring elevation but as soon as the progress bar ends and it displays the final output it crashes (and thus I can't see the final output), with shell execution off (executing via the main window) it either doesn't show the progress bar at all and just shows the final output, or shows nothing, or it does show the progress bar, but with each increment it's a new line which obviously doesn't look right...
this is my code:
public static void ExecuteCommand(string command) {
Process prc = new Process();
ProcessStartInfo info = new ProcessStartInfo
{
Verb = "runas",
FileName = "cmd.exe",
UseShellExecute = true,
Arguments = "/c" + command
};
prc.StartInfo = info;
prc.Start();
prc.WaitForExit();
prc.Close();
}
if anyone can think of a fix please do tell me, I have been stuck on this for about 3 days...
Turns out the culprit was "/c", if I change it to "/K" the windows stays open until I close it manually like I wanted.
Many examples and MSDN are using a new Process to get the exitcode, however, creat a new variable looks not so grace.So, I tried this
Process.Start("Application.exe", "parameter").WaitForExit().ExitCode
aimed to get the exitcode in one line but failed.
And is there any solution of this writing?
It doesn't work like that because WaitForExit() returns a bool, which doesn't have an ExitCode property. If you want this behavior on one line, you'll have to implement it yourself in a method and call it.
public int RunAndGetCode(string executable, string parameter)
{
var process = Process.Start(executable, parameter).WaitForExit();
return process.ExitCode;
}
// then later
var code = RunAndGetCode("Application.exe", "parameter");
So... That's not quite how Process works. You could write a wrapper class that allows you to do it in one one line, or using a using block, but when you have to wait for any process, that means you're locking up your own process while waiting for it. In Windows that is terrible practice.
The way it's designed in C#, it allows your own process to do other work while the process you called has returned. (Wrote this on mobile device; apologies for errors)
So, in short, no, but I see nothing wrong with this:
Process p = new Process();
P.Start();
While(!p.WaitForExit()) {
//do work while you wait for the calling process to return
}
var exitCode = p.ExitCode
I'm trying to make a C# program that can listen for and output to cmd.exe and log it to file. For example, if I run an exe and it runs a command in cmd like echo "hello", I want echo "hello" to be written in a file.
I know I need to use FileSystem, as well as Process maybe?
If this is even possible, and help would really be appreciated. Thanks.
Here's a quick little example that should work. There are a lot of examples out there, I'll try looking on stackoverflow and post one as well...
string cmd_to_run = "dir"; // whatever you'd like this to be...
// set up our initial parameters for out process
ProcessStartInfo p_info = new ProcessStartInfo();
p_info.FileName = "cmd";
p_info.Arguments = "/c " + cmd_to_run;
p_info.UseShellExecute = false;
// instantiate a new process
Process p_to_run = new Process();
p_to_run.StartInfo = p_info;
// wait for it to exit (I chose 120 seconds)
// waiting for output here is not asynchronous, depending on the task you may want it to be
p_to_run.Start();
p_to_run.WaitForExit(120 * 1000);
string output = p_to_run.StandardOutput.ReadToEnd(); // here is our output
Here's the Process class MSDN overview (there's a quick example on this page): https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx
And here's a SO example dealing with calling ReadToEnd() on Process: StandardOutput.ReadToEnd() hangs
I need to execute external program with arguments and get result from it (~1000 times with different arguments).
I found solution like this:
using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "qwe 123";
process.Start();
process.WaitForExit();// Waits here for the process to exit.
// And check exit code for result
I need many repetitions with different arguments, but this code every time initiate new process. It is very expensive operation. I think, i can "load" ("save") process and repeat it without everytime initiating.
Or maybe exist other way to solve this problem?
If the started process is under your control it will be much more efficient to pass the input not via command line parameter for a new process instance every time, but re-program it so that the process reads its standard input line-wise in a loop, processes each line, and writes the result to its standard output. It's easy to hook up the calling program to the process' input and output:
// ...
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
// ...
process.Start();
StreamReader results = process.StandardOutput;
StreamWriter processInput = process.StandardInput;
foreach( var arg in args )
{
processInput.WriteLine(arg);
var oneResult = results.ReadLine();
// do something with this oneResult
}
This example assumes that each argument fits in one line (and each result fits in a line, too). Writing and reading a single line each time is our simple "protocol" for knowing when to start processing (on the process side) and when the result is complete (on the C# side).
I should perhaps add that a real program should add error handling and e.g. evaluate Process.Start()'s return value.