C#
process starting
programming
coding tutorial
.NET framework

How do I start a process from C?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, starting another process is done through System.Diagnostics.Process. The simplest call opens an executable or document, while the more practical pattern uses ProcessStartInfo so you can control arguments, working directory, shell behavior, and standard-output redirection.

Simple Process Start

The shortest form is:

csharp
using System.Diagnostics;

Process.Start("notepad.exe");

That is convenient for a quick local tool launch, but it is limited. As soon as you need arguments or output capture, use ProcessStartInfo.

Starting a Process With Arguments

csharp
1using System.Diagnostics;
2
3var startInfo = new ProcessStartInfo
4{
5    FileName = "dotnet",
6    Arguments = "--info",
7    UseShellExecute = false
8};
9
10using var process = Process.Start(startInfo);
11process?.WaitForExit();

This explicitly launches dotnet --info and waits for it to finish.

Capturing Standard Output

If you want to read the output of the child process, disable shell execution and redirect the streams:

csharp
1using System;
2using System.Diagnostics;
3
4var startInfo = new ProcessStartInfo
5{
6    FileName = "dotnet",
7    Arguments = "--version",
8    UseShellExecute = false,
9    RedirectStandardOutput = true,
10    RedirectStandardError = true
11};
12
13using var process = Process.Start(startInfo);
14
15string stdout = process!.StandardOutput.ReadToEnd();
16string stderr = process.StandardError.ReadToEnd();
17
18process.WaitForExit();
19
20Console.WriteLine("STDOUT: " + stdout);
21Console.WriteLine("STDERR: " + stderr);

This is the usual pattern for wrappers around command-line tools.

Opening a File or URL

If your goal is to let Windows open a file or URL with its default handler, shell execution is often the right choice:

csharp
1using System.Diagnostics;
2
3Process.Start(new ProcessStartInfo
4{
5    FileName = "https://example.com",
6    UseShellExecute = true
7});

The same approach works for documents such as PDFs if the system has an associated application.

Working Directory and Hidden Windows

For command-line tools, setting the working directory often matters because relative paths are resolved from there.

csharp
1var startInfo = new ProcessStartInfo
2{
3    FileName = "dotnet",
4    Arguments = "build",
5    WorkingDirectory = @"C:\Projects\Demo",
6    UseShellExecute = false,
7    CreateNoWindow = true
8};

CreateNoWindow is useful for background utility processes where you do not want an extra console window flashing on screen.

Waiting and Exit Codes

If the child process matters to your program flow, wait for it and inspect the exit code:

csharp
1using System;
2using System.Diagnostics;
3
4var startInfo = new ProcessStartInfo
5{
6    FileName = "cmd.exe",
7    Arguments = "/c exit 5",
8    UseShellExecute = false
9};
10
11using var process = Process.Start(startInfo);
12process!.WaitForExit();
13
14Console.WriteLine(process.ExitCode);

That is important for build tools, scripting wrappers, and deployment utilities.

Handle Failures Explicitly

Starting a process can fail before the child even runs. Wrap startup in normal exception handling if the executable path or shell association may be missing:

csharp
1try
2{
3    Process.Start("missing-tool.exe");
4}
5catch (Exception ex)
6{
7    Console.WriteLine(ex.Message);
8}

That makes launcher code far easier to diagnose than assuming Process.Start always succeeds.

Common Pitfalls

The most common mistake is forgetting UseShellExecute = false when redirecting standard output or error. Redirection requires shell execution to be disabled.

Another issue is building command strings unsafely. If file names or arguments come from external input, pass them carefully and avoid naive concatenation.

A third pitfall is reading redirected output incorrectly and risking deadlocks in more complex scenarios. For simple commands, ReadToEnd() is fine, but long-running tools may need asynchronous reading.

Finally, do not assume Process.Start always succeeds silently. The executable may not exist, permissions may be wrong, or the target machine may not have the expected shell association.

Summary

  • Use Process.Start for basic process launching in C#.
  • Prefer ProcessStartInfo when you need arguments, redirection, or shell control.
  • Set UseShellExecute = false if you want to capture output.
  • Wait for exit and inspect the exit code when the child process result matters.
  • Treat argument construction and missing executables as normal failure cases to handle.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.