start a process with arguments in csharp

Here's an example to start a process with arguments in C#:

main.cs
using System.Diagnostics;

// specify the path and arguments of the process to start
var processStartInfo = new ProcessStartInfo
{
    FileName = "myapp.exe",
    Arguments = "arg1 arg2",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true
};

// start the process and wait for it to exit
using (var process = new Process { StartInfo = processStartInfo })
{
    process.Start();
    process.WaitForExit();
    string output = process.StandardOutput.ReadToEnd();
    // do something with the output
}
538 chars
21 lines

In this example, we create a new instance of the ProcessStartInfo class, which allows us to specify the path of the executable to start and any arguments to pass to it. We then create a new Process object and set its StartInfo property to the ProcessStartInfo object we just created.

We start the process by calling the Start method, and then wait for it to exit by calling the WaitForExit method. If the process generates any output, we can read it from its StandardOutput stream.

Note that we've also set a few properties on the ProcessStartInfo object to redirect the process's output to our program's standard output and hide the process's window. This is optional, but can be useful in certain scenarios.

gistlibby LogSnag