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.
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:
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
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:
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:
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.
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:
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:
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.Startfor basic process launching in C#. - Prefer
ProcessStartInfowhen you need arguments, redirection, or shell control. - Set
UseShellExecute = falseif 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
- How do I stop a thread when my winform application closes
- How do I submit disabled input in ASP.NET MVC?
- How do I sync the SVN revision number with my ASP.NET web site?
- How do I test an async method with NUnit or possibly with another framework?
- How do I trap CtrlC SIGINT in a C console app?
- How do I truncate a .NET string?
- How do I turn a C# object into a JSON string in .NET?
- How do I turn a C object into a JSON string in .NET?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.