Process Class in C#

The Process class is in the System.Diagnostics namespace that has methods to run a .exe file to see any document or a webpage.

The Process class provides Start methods for launching another application in the C# Programming.

Example: The following program opens the C: Drive:

using System;
using System.Diagnostics;
class program
{
    static void Main()
    {
        Process.Start("c:\\");
    }
}

Explanation

To use the Process class, we need to declare the System.Diagnostics namespace. The Start method of the Process class sends the "C:\" directory name to the operating system resulting in the opening of C:\ Drive in Windows Explorer.

Example:The following program opens Notepad and WordPad.

using System;
using System.Diagnostics;
using System.Threading;
class program
{
    static void Main()
    {
        Process.Start("notepad");
        //Wordpad open after 1 sec
        Thread.Sleep(1000);
        Process.Start("wordpad");
    }
}

Example: The following program opens a text file.

using System;
using System.Diagnostics;
using System.Threading;
class program
{
    static void Main()
    {
        //this will open anoop.txt file present in f drive
        Process.Start("F:\\anoop.txt");
    }
}

Example: The following program opens a Website URL.

using System;
using System.Diagnostics;
using System.Threading;
class program
{
    static void Main()
    {
        //open c-sharpcorner website
        Process.Start("http://www.c-sharpcorner.com");
    }
}

Preview

Opening a Website URL

Example: The following Windows Forms program opens the Calculator Acccessory (Calc.exe).

using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Process.Start("calc.exe");
        }
    }
}

Preview

Example: The following program searches content using Google.

using System;
using System.Diagnostics;
using System.Threading;
class program
{
    static void Main()
    {
        Console.Write("Search it on Google:");
        //Taking input for searching
        string str = Console.ReadLine();
        SearchItOnGoogle(str);
    }
    public static void SearchItOnGoogle(string text)
    {
        Process.Start("http://google.com/search?q=" + text);
    }
}

Preview

search content on Google

Search Result


Recommended Ebook

Programming Strings using C#

Download Now!
Similar Articles