46

How do I run an external program like Notepad or Calculator via a C# program?

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
ahqing
  • 469
  • 1
  • 4
  • 3
  • 4
    Welcome 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
  • 1
    Possible 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 Answers4

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
  • 13
    Remember to dispose the process or use it in a `using(Process pProcess = new Process()) { }` block – pKami Sep 21 '16 at 17:43
32

Use System.Diagnostics.Process.Start

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
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