Ralph
Ralph

Reputation: 63

c# - Opening chosen file with associated App

I have successfully prompted user to select a file in C# using

the openFileDialog control.

I now have the filename, lets call it foo.docx

I want to open the file with the asssoicated app.

i.e., if it is a docx file, launch with word.

Is there a best way to just pass the filename and it do the launch ?

I used System.Diagnostics.Process.Start(openFileDialog1.FileName.ToString());

TIA.

Ralph

Upvotes: 3

Views: 345

Answers (2)

Oded
Oded

Reputation: 499302

Just call Process.Start with the file name - the OS will select the associated application.

Process.Start(@"path to\foo.exe");

Upvotes: 1

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60724

Simply use

Process.Start(filename);

This will open the program in the default program set in Windows.

Also, you can use the same to open a URL in the user's default browser.

Upvotes: 4

Related Questions