Open a file with the system’s default application in C#

invoke a file's default application

Sometimes you might want your program to use a default application to open a file. For example, you might want to display a PDF file, web page or URL on the internet.

The System.Diagnostics.Process class’s Start method starts the application that your system associates with a file. For example, if the file has a .txt extension, then the system opens the file in NotePad, WordPad, or whatever program is associated with .txt files.

Similarly if the file has a .html extension, the system opens it in your system’s default browser. Note that the file can be a URL such as http://www.csharphelper.com or even just www.csharphelper.com.

In this example, when you select a file name from the ComboBox or type a file name of your own and click Open, the program uses the following code to open the file.

// "Start" the file.
private void btnOpen_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start(cboFile.Text);
}


Download Example   Follow me on Twitter   RSS feed


This entry was posted in files, programs, system and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Open a file with the system’s default application in C#

  1. Truong says:

    thanks

  2. Oleg says:

    Process.Start(“file.txt”) – gives

    System.ComponentModel.Win32Exception: ‘The specified executable is not a valid application for this OS platform.’

    • RodStephens says:

      I don’t know why it would say that. I assume you’re running Windows?

      Is there an application registered to open .txt files on our system? I.e. what happens if you double-click a .txt file? That should be what happens when you use Process.Start.

Comments are closed.