How to start a process or a program in C#

In the following example we will see how to open a program or start a process in C# by using the Process class. We start Microsoft Word.

start process or program in c#

First we add the directive  "using System.Diagnostics;"

using System.Diagnostics;

Then in button click event we add this code:

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

If we want to start a Microsoft Word (.docx) file, we use an overload of Start method that accepts a ProcessStartInfo object. The code below describes how to open a (.docx) file:

private void button1_Click(object sender, EventArgs e)
{
    string ProgramName = "winword.exe";
    Process process = new Process();
    process.StartInfo.FileName = ProgramName;
    process.StartInfo.Arguments="D:\\MyDocument.docx";
    process.Start();
}
How to start a process or a program in C# How to start a process or a program in C# Reviewed by Bloggeur DZ on 11:21 Rating: 5

Aucun commentaire:

Fourni par Blogger.