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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With