0

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.

Indigo
  • 2,887
  • 11
  • 52
  • 83
  • 1
    As one person who really dislikes that Excel is MDI, don't do #2. Let the system decide if it should open the file in a second window or not. Excel can be configured to NOT open new files in MDI format and what you are proposing would change the users expected behavior. – NotMe Oct 08 '13 at 16:50

2 Answers2

3

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).

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • This is exactly what I did at first place. But then I was wondering how to actually know which application is that and give a specific message to the user. – Indigo Oct 08 '13 at 16:47
  • Thanks @Ken White, about the Edit just now, this is exactly what I needed to know. I will go with the normal way as you have given. – Indigo Oct 08 '13 at 16:52
  • Um... Wouldn't the user already know what their default application was for opening the file? After all, they'll be looking right at the application once the sheet opens. But anyway, that question would be a duplicate of [this one](http://stackoverflow.com/q/162331/62576). – Ken White Oct 08 '13 at 16:53
  • Yes, indeed. It was just an idea to let the user know that the application understands what it is doing. But I guess that would be unnecessary effort. – Indigo Oct 08 '13 at 17:04
  • i know this is a pretty old Question. But im kinda stuck. If i try the same opening a XLSX from my destop i get the following error message: System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.' My code: ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\\Users\bschr\Desktop\PriceCheckExport.xlsx"); Process.Start(startInfo); – Benjamin Schröder Feb 06 '21 at 10:00
  • @BenjaminSchröder: Do you have Excel installed on that computer? Can you start the same .xlsx file by double-clicking it in Windows Explorer? – Ken White Feb 07 '21 at 01:21
  • @BenjaminSchröder: What platform are you building your app for (32 or 64 bit)? What platform is your Office version? – Ken White Feb 15 '21 at 22:55
0

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!

sab669
  • 3,984
  • 8
  • 38
  • 75