Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run external program via a C# program?

Tags:

c#

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

like image 535
ahqing Avatar asked Jul 04 '10 05:07

ahqing


4 Answers

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();
}
like image 139
vitor_gaudencio_oliveira Avatar answered Oct 09 '22 17:10

vitor_gaudencio_oliveira


Use System.Diagnostics.Process.Start

like image 21
Mitch Wheat Avatar answered Oct 09 '22 18:10

Mitch Wheat


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();
        }
    }
}
like image 25
Ramakrishnan Avatar answered Oct 09 '22 19:10

Ramakrishnan


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.

like image 32
Incognito Avatar answered Oct 09 '22 19:10

Incognito