Title: Open a file with the system's default application in C#
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 the example to experiment with it and to see additional details.
|