How do I run an external program like Notepad or Calculator via a C# program?
Asked
Active
Viewed 1.1e+01k times
46
-
4Welcome to Stack Overflow. I think it's safe to assume that English is your second language. To increase your chances of getting an answer, I would re-write the question title to say "How to open an external program from a C# program?". Also is it a Console App, Winforms, Web(hopefully not)? Provide a little more information, and make sure you check out the Stack Overflow FAQ. – Marko Jul 04 '10 at 05:09
-
@Michael I assume hw is simply how. – Mathias Jul 04 '10 at 05:11
-
1Possible duplicate of [How do I start a process from C#?](http://stackoverflow.com/questions/181719/how-do-i-start-a-process-from-c) – Gustavo Mori Nov 09 '16 at 00:27
4 Answers
62
Maybe it'll help you:
using(System.Diagnostics.Process pProcess = new System.Diagnostics.Process())
{
pProcess.StartInfo.FileName = @"C:\Users\Vitor\ConsoleApplication1.exe";
pProcess.StartInfo.Arguments = "olaa"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
}

Vítor Oliveira
- 1,851
- 18
- 23
-
13Remember to dispose the process or use it in a `using(Process pProcess = new Process()) { }` block – pKami Sep 21 '16 at 17:43
32

StayOnTarget
- 11,743
- 10
- 52
- 81

Mitch Wheat
- 295,962
- 43
- 465
- 541
-
FYI "Example" is a dead link. Always the drawback when specifying links. – AndersK Mar 30 '15 at 06:19
-
-
2Not dead in the sense that it doesnt lead anywhere, dead in the sense that it doesnt actually show an example. – AndersK Mar 30 '15 at 06:39
15
Hi this is Sample Console Application to Invoke Notepad.exe ,please check with this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Demo_Console
{
class Program
{
static void Main(string[] args)
{
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "Notepad.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();
}
}
}

Ramakrishnan
- 5,254
- 9
- 34
- 41
13
For example like this :
// run notepad
System.Diagnostics.Process.Start("notepad.exe");
//run calculator
System.Diagnostics.Process.Start("calc.exe");
Follow the links in Mitchs answer.

Incognito
- 16,567
- 9
- 52
- 74