Click here to Skip to main content
15,868,076 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

On Windows, given a file (with its filepath), how can I retrieve the program used to open it?

For example, given a .PDF file, my program would know that this file should be handled by a PDF viewer, and return mes the filepath of the Adobe Reader installed in my machine.

Or given a .DOC file, my program would know that this file should be handled by MS Word, and gives me the filepath of the MS Word installed somewhere ...

This information can be retrieved from console, with 2 commands "assoc" and "ftype". But I dont know how to do so from my C program.

I looked around for a long time, but got no answer. Any help would be highly appreciated.

Thanks,
NX
Posted

Let's assume you know how to read the registry. You can get all the functions you need here:
http://msdn.microsoft.com/en-us/library/ms724875.aspx[^].

Let's say you have the extension ".ext" (substitute the value of your variable holding the extension parameter, with starting dot). I will illustrate the procedure using my system on the example of the extension ".pdf".
Here is the pseudo-code:

  1. Get to the registry key HKEY_CLASSES_ROOT\.pdf
  2. Get default value for this key; assign to some variable key_default. On my system, key_default == "AcroExch.Document"
  3. Get to the registry key HKEY_CLASSES_ROOT\, concatenate it with key_default. On my system, it is "HKEY_CLASSES_ROOT\AcroExch.Document"
  4. Go to the sub-key of the key found on the previous step by adding "\Shell\Open\Command" (concatenate from right). On my system it's "HKEY_CLASSES_ROOT\AcroExch.Document\Shell\Open\Command".
  5. Get default value for this key. This is a command line used to open the file with the registered extension using the Shell. On my system, it is a full path the Adobe acrobat viewer: "..\\AcroRd32.exe" "%1". The parameter "%1" is given in the format of Windows batch file. It means that if you put the whole string in the batch file, you can call this batch with the parameter representing the full data file name; the file should be of the type defined by the extension you've started with.
  6. To load the file with the application registered for a give file extension you need to run the application using the path name with the parameter specifier "%1" substituted with the actual file name.


Problem solved.

—SA
 
Share this answer
 
v2
Comments
ShilpiP 23-May-11 2:56am    
Nice explanation. Have +5 :)
Sergey Alexandrovich Kryukov 23-May-11 18:42pm    
Thank you very much. Hope it's enough to write the code in any language.
--SA
ShilpiP 24-May-11 1:15am    
Yes it is enough. I did File and Shell association for all operating system (XP, Windows 7 and Vista)and have idea about it.For me, your answer is perfect.

Sergey Alexandrovich Kryukov 24-May-11 21:47pm    
Thank you, SP.
--SA
The quick, short, easy and nice way is to use FindExecutable() on the file. So there is no need to mess up with the registries :-)

Thanks,
N
 
Share this answer
 
have a look in and around http://msdn.microsoft.com/en-us/library/ee872121(v=VS.85).aspx[^]

It talks about file and application registration in the shell
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900