Take the 2-minute tour ×

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

share|improve this question
If you already got it working, what's the question? –  David Grayson Feb 2 '12 at 16:14

5 Answers 5

up vote 9 down vote accepted

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.

share|improve this answer
I must be tired, thanks that did it! –  Valmond Feb 3 '12 at 8:43


So you need to substitute "c:\\windows\\notepad.exe" with the actual file you want to open and leave lpParameters null.

share|improve this answer

See Launching Applications:

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

On windows, a good memory hook is to think of all data-files being executable by the shell. You can also try it out in a command box, where you can just type a filename, and it will be opened up. Or, the other way around, every file in Windows can be opened, and the default opening-action for executable files is to execute them.

share|improve this answer

According to the MS Knowledge Base, ShellExecute should work (we do this in Delphi all the time):

ShellExecute(Handle, "Open", Filename, "", "C:\", SW_SHOWNORMAL)
share|improve this answer

Maybe try start instead of open?

share|improve this answer
open is the default verb that Explorer uses when you double-click a file. You can leave the parameter NULL to use the actual default for the file type. –  Mark Ransom Feb 2 '12 at 16:12

Your Answer


