Start an External Blank Application or a File From Your C# Application

Something we need to learn, because it is necessary to know, is how to launchi external applications from our C# application. In other words, how to open files that have an associated application or an application that we choose, for example opening a txt file with Microsoft Word or Notepad.

We need to use in this case the namespace "System.Diagnostics" and specifically the class Process, look at the following sample code that we will explain later:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Processs
{
    public partial class Form1 :
Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process.Start("WINWORD.exe");
// launch a blank word applicatio
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Process.Start("EXCEL.exe");
// launch a blank excel applicatio
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Process.Start("WINWORD.exe","hello.docx");
//launch a Hello word file with ms word
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Process.Start("EXCEL.exe","hello.xlsx");
//launch a Hello excel file with ms Excel
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Process.Start("WINWORD.exe", "hello.txt");
//launch a Hello TXT file with ms word
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Process.Start("NOTEPAD.exe", "hello.txt");
//launch a Hello TXT file with notepad
        }
 
        private void button8_Click(object sender, EventArgs e)
        {
            Process.Start( "vs2010.pdf");
//launch a PDF with the default associated application
        }

        private void button9_Click(object sender, EventArgs e)
        {

            Process process = Process.Start("notepad.exe");
            System.Threading.Thread.Sleep(2000);// start and kill a process after 2 seconds
            process.Kill();
 
        }
    }
}

So as you can see you can start a process by the method  Process.start() by pecifying the file or the file and the application to associate it with, don't forget to add  the directive System.Diagnostics to your application .

Before I finish, you can end and kill the process you started, this is what I demonstrated in the last peoce of the code, try to download the source to understand better, wish you luck and leave a comment please.


Similar Articles