How to set an application as the default program of opening a certain type of file programmatically?

ぃ、小莉子 提交于 2019-12-10 15:25:28

问题


There's an executable file generated from my program in MFC and I want to use it as the default program to open the .jpg files. That is to say, each time I double click a .jpg file, my program will run.

I tried to add some registry entries linking .jpg files with my program, such as HKEY_CLASSES_ROOT\.jpg\shell\open\command (set its value to "myProgram.exe" "%1"), and HKEY_CLASSES_ROOT\myProgram.

The method works just fine except when some other applications register themselves to open the .jpg files. For example, I have installed acdSee on my computer, so each time I doule click a .jpg file, it always start acdSee instead of my own program. But when I register a completely new type of file with my program, it can be open in the program. I don't know how to set my program as the default opening program of an already registered file programmatically. Can anyone help me solve this problem? Thank you very much!


回答1:


The more typical/standard way for doing this is to set the default value of the ".jpg" key to a name that identifies the file type more clearly, and then setup the various associated actions there. So for jpgs, you might do this:

HKCR\.jpg
   @default = MyApp.JpegImage
HKCR\MyApp.JpegImage\shell\open\command
   @default = "myApp.exe "%1""

If some other program decides to register the type, they will replace the default value for HKCR.jpg with some other value, like OtherProgram.Jpg. At that point, you could re-register it to your app by setting the value back to MyApp.JpegImage.

Disclaimer: When making this sort of change, please also try to respect the user's preferences. For instance, when installing your application, give the user the option to set this file association or not set it. You can also provide a command from inside your installed application to reset the associations, if the user should wish to do so.

If you instead wanted to add some additional commands to an existing registered type, you would read the default value of the .jpg key to find the name of the file type. Then you could open that key and add an action to the existing set of actions. For instance, you could add the following:

HKCR\ExistingApp.JpegImage\shell\myopen\
    @default = "Open with MyApp"
HKCR\ExistingApp.JpegImage\shell\myopen\command\
    @default = "myApp.exe "%1""



回答2:


Note that by writing a key to HKCR, you're actually writing to HKLM\Software\Classes. This will require administrative privileges. However, you can make per-user changes within a user context by writing your keys to HKCU\Classes\Root instead.

Also, user preferences in HKCU will override the system defaults in HKLM, which sounds like what your problem might be.

This is when a program has not registered an extension as a "Default" (Is the program listed in Set Programs and Defaults in the Control Panel?)

Time to start reading documentation!



来源:https://stackoverflow.com/questions/316204/how-to-set-an-application-as-the-default-program-of-opening-a-certain-type-of-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!