Indigo
Indigo

Reputation: 3007

Open Excel files using default application on windows system

After creating an Excel file (*.xlsx) programmatically, I would like to open it using the default system application for *.xlsx files.

Problems are,

  1. Getting that default application whether it is Microsoft Office Excel or some third party software that can open *.xlsx files. Considering, normally user can set default applications to open a specific file in Windows (version 7, 8 or 8.1)
  2. Now check if that application is already running on the system and if yes use or then open a new instance of it to open *.xlsx file.

This question can also be generalized as to open any file using the default applications set for it in windows system and give a message to the user in case there isn't any applications that can open this file on the local system.

Upvotes: 0

Views: 3781

Answers (2)

Ken White
Ken White

Reputation: 125748

Use Process.Start, and it will automatically open in the default application for that file extension. You'll find it in System.Diagnostics.

ProcessStartInfo startInfo = new ProcessStartInfo("C:\\SomePath\Test.xlsx");
Process.Start(startInfo);

As far as the second problem, there's no way to do that reliably. If you were sure the default application was Excel, you could use the Interop libraries to do so via COM, but there's no way to tell if the alternative default application supports that (or any other way of interop).

Upvotes: 3

sab669
sab669

Reputation: 4104

Can't you just use Process.Start(myFile.xlsx)? It'll basically just try to tell Windows decide what to do with it.

edit; Heh, darn- beaten by a few seconds. His answer is way more helpful!

Upvotes: 0

Related Questions