Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you open a file with the program associated with its file extension?

Tags:

Is there a simple way to open a file by its associated program in windows? (like double clicking it in windows explorer but done automatically with my code)

For example, on computer A, "text.txt" will be opened in wordpad but on computer B it will be opened by Notepad++ because of the users file extension assignments.

I tried ShellExecute

ShellExecute(0, L"open", L"c:\\windows\\notepad.exe" ,L"c:\\outfile.txt" , 0 , SW_SHOW ); 

which works but if I omit the notepad.exe parameter weird things happen (a random explorer is shown).

like image 991
Valmond Avatar asked Feb 02 '12 16:02

Valmond


People also ask

How do I open a file with the file extension file?

If you know what type of file your FILE file contains, you can rename the file to use that extension. For example, if you know your FILE file contains a Microsoft Word document, you can rename it to use the . DOCX extension. You'll then be able to open the file in Microsoft Word.

How can you open a file in a program that uses a different format?

From the desktop, right-click the desired file, select Open with, and click Choose another app from the menu that appears. Select the desired application. If you don't see the one you want, click More apps or Look for an app in the Store to look for other applications.


1 Answers

You want to use the file to open as the file argument, not the parameter argument. No need to specify which program to use, ShellExecute will look it up for you.

ShellExecute(0, 0, L"c:\\outfile.txt", 0, 0 , SW_SHOW ); 

By leaving the verb as NULL (0) rather than L"open", you get the true default action for the file type - usually this is open but not always.

like image 71
Mark Ransom Avatar answered Sep 29 '22 21:09

Mark Ransom