如何将应用程序设置为以编程方式打开某种类型文件的默认程序? [英] How to set an application as the default program of opening a certain type of file programmatically?

查看:51
本文介绍了如何将应用程序设置为以编程方式打开某种类型文件的默认程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序在 MFC 中生成了一个可执行文件,我想用它作为默认程序来打开 .jpg 文件.也就是说,每次我双击一个.jpg文件,我的程序就会运行.

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.

我尝试添加一些将 .jpg 文件链接到我的程序的注册表项,例如 HKEY_CLASSES_ROOT\.jpg\shell\open\command(将其值设置为 "myProgram.exe" "%1") 和 HKEY_CLASSES_ROOT\myProgram.

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.

该方法工作正常,除非某些其他应用程序注册自己以打开 .jpg 文件.例如,我在我的电脑上安装了acdSee,所以每次我双击一个.jpg文件时,它总是启动acdSee而不是我自己的程序.但是当我用我的程序注册一个全新类型的文件时,它可以在程序中打开.我不知道如何以编程方式将我的程序设置为已注册文件的默认打开程序.谁能帮我解决这个问题?非常感谢!

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!

推荐答案

执行此操作的更典型/标准方法是将.jpg"键的默认值设置为更清楚地标识文件类型的名称,然后在那里设置各种相关的操作.所以对于 jpg,你可以这样做:

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""

如果其他程序决定注册该类型,它们会将 HKCR.jpg 的默认值替换为其他值,例如 OtherProgram.Jpg.此时,您可以通过将值设置回 MyApp.JpegImage 将其重新注册到您的应用.

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.

如果您想向现有注册类型添加一些额外的命令,您可以读取 .jpg 键的默认值以查找文件类型的名称.然后您可以打开该键并向现有操作集添加一个操作.例如,您可以添加以下内容:

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""

这篇关于如何将应用程序设置为以编程方式打开某种类型文件的默认程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆