Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 956
Martijn Avatar asked Jun 08 '15 16:06

Martijn


People also ask

How do I associate an app with a file?

Open File Manager and browse the the folder containing the files that you want to associate with a particular app. This will set the target file types to be opened with the selected app and open that file in that app as well. By the way doing things in Android is not too difficult.

What does register file associations mean?

File associations are registry settings that tell Windows what application to use to open files of a certain type. For example, Windows typically launches Notepad.exe when a text (. txt) file is opened.

How do you Unassociate a file type?

To be more precise, run the app (no installation needed), choose File Type Settings -> click Context Menu -> find your extension -> click Next -> choose Open -> click Edit Selected Command... -> clear the Program path -> click Next -> click Save Context Menu.


1 Answers

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.

like image 78
David Heffernan Avatar answered Nov 06 '22 16:11

David Heffernan