ahqing
ahqing

Reputation: 489

How to run external program via a C# program?

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

Upvotes: 48

Views: 118104

Answers (4)

Vítor Oliveira
Vítor Oliveira

Reputation: 2091

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();
}

Upvotes: 64

Mitch Wheat
Mitch Wheat

Reputation: 300529

Use System.Diagnostics.Process.Start

Upvotes: 31

Ramakrishnan
Ramakrishnan

Reputation: 5436

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();
        }
    }
}

Upvotes: 15

Incognito
Incognito

Reputation: 16577

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.

Upvotes: 14

Related Questions