How do I start a process from C#?

C#.NetWindowsProcessprocess.start

C# Problem Overview


How do I start a process, such as launching a URL when the user clicks a button?

C# Solutions


Solution 1 - C#

As suggested by Matt Hamilton, the quick approach where you have limited control over the process, is to use the static Start method on the System.Diagnostics.Process class...

using System.Diagnostics;
...
Process.Start("process.exe");

The alternative is to use an instance of the Process class. This allows much more control over the process including scheduling, the type of the window it will run in and, most usefully for me, the ability to wait for the process to finish.

using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.

This method allows far more control than I've mentioned.

Solution 2 - C#

You can use the System.Diagnostics.Process.Start method to start a process. You can even pass a URL as a string and it'll kick off the default browser.

Solution 3 - C#

Just as Matt says, use Process.Start.

You can pass a URL, or a document. They will be started by the registered application.

Example:

Process.Start("Test.Txt");

This will start Notepad.exe with Text.Txt loaded.

Solution 4 - C#

I used the following in my own program.

Process.Start("http://www.google.com/etc/etc/test.txt")

It's a bit basic, but it does the job for me.

Solution 5 - C#

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/YourSubDirectory/yourprogram.exe");
Process.Start(new ProcessStartInfo(path));

Solution 6 - C#

class ProcessStart
{
    static void Main(string[] args)
    {
        Process notePad = new Process();

        notePad.StartInfo.FileName   = "notepad.exe";
        notePad.StartInfo.Arguments = "ProcessStart.cs";

        notePad.Start();
    }
}

Solution 7 - C#

Use the Process class. The MSDN documentation has an example how to use it.

Solution 8 - C#

You can use this syntax for running any application:

System.Diagnostics.Process.Start("Example.exe");

And the same one for a URL. Just write your URL between this ().

Example:

System.Diagnostics.Process.Start("http://www.google.com");

Solution 9 - C#

If using on Windows

Process process = new Process();
process.StartInfo.FileName = "Test.txt";
process.Start();

Works for .Net Framework but for Net core 3.1 also need to set UseShellExecute to true

Process process = new Process();
process.StartInfo.FileName = "Test.txt";
process.StartInfo.UseShellExecute = true;
process.Start();

Solution 10 - C#

Declare this

[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
[DllImport("user32")]
private static extern bool ShowWindowAsync(IntPtr hwnd, int a);

And put this inside your function (note that "checkInstalled" is optional, but if you'll use it, you have to implement it)

if (ckeckInstalled("example"))
{
    int count = Process.GetProcessesByName("example").Count();
    if (count < 1)
        Process.Start("example.exe");
    else
    {
        var proc = Process.GetProcessesByName("example").FirstOrDefault();
        if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
        {
            SetForegroundWindow(proc.MainWindowHandle);
            ShowWindowAsync(proc.MainWindowHandle, 3);
        }
    }
}

NOTE: I'm not sure if this works when more than one instance of the .exe is running.

Solution 11 - C#

Include the using System.Diagnostics;.

And then call this Process.Start("Paste your URL string here!");

Try something like this:

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace btnproce
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string t ="Balotelli";
            Process.Start("http://google.com/search?q=" + t);
        }
    }
}

Please note that it is a sample ASP.NET page as an example. You should try and improvise a little bit.

Solution 12 - C#

To start Microsoft Word for example, use this code:

private void button1_Click(object sender, EventArgs e)
{
    string ProgramName = "winword.exe";
    Process.Start(ProgramName);
}

For more explanations, check out this link.

Solution 13 - C#

You can use this syntax:

private void button1_Click(object sender, EventArgs e)  {
   System.Diagnostics.Process.Start(/*your file name goes here*/);
}

Or even this:

using System;
using System.Diagnostics;
//rest of the code

private void button1_Click(object sender, EventArgs e)  {
   Process.Start(/*your file name goes here*/);
}

Both methods would perform the same task.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWanKenoobiView Question on Stackoverflow
Solution 1 - C#Andy McCluggageView Answer on Stackoverflow
Solution 2 - C#Matt HamiltonView Answer on Stackoverflow
Solution 3 - C#GvSView Answer on Stackoverflow
Solution 4 - C#BlackvaultView Answer on Stackoverflow
Solution 5 - C#SimperTView Answer on Stackoverflow
Solution 6 - C#Ravi Kumar G NView Answer on Stackoverflow
Solution 7 - C#Franci PenovView Answer on Stackoverflow
Solution 8 - C#alireza aminiView Answer on Stackoverflow
Solution 9 - C#Moon WaxingView Answer on Stackoverflow
Solution 10 - C#Carla JesusView Answer on Stackoverflow
Solution 11 - C#AVIK DUTTAView Answer on Stackoverflow
Solution 12 - C#user4340666View Answer on Stackoverflow
Solution 13 - C#NoNAMEView Answer on Stackoverflow