How to execute a file with a different extension's associated application?

752 views Asked by At

In my application, I would like to be able to execute a file with an arbitrary name and extension using the program associated with a different extension.

For example, say I have a file called file.dat, but I wanted to open it with Notepad, as if it was named file.txt or file.dat.txt. Also, I don't have permission to rename the file, so that's out of the question.

If the file were called file.txt, I could call ShellExecute. But that fails when calling file.dat; Windows complains that there’s no association for that file.

I don't want to hardcode a specific executable, because (in the example above) the end user could have associated .txt files with Wordpad instead of Notepad. In such a case, I’d want my file.dat to be opened with Wordpad.

1

There are 1 answers

0
David Heffernan On BEST ANSWER

Call ShellExecuteEx and specify the lpClass member of the SHELLEXECUTEINFO struct. Note that you must include SEE_MASK_CLASSNAME in the fMask member.

For instance, set lpClass to '.txt' to request that the file be opened with the program associated with the .txt extension.