windows 如何将应用程序设置为以编程方式打开某种类型文件的默认程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/316204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set an application as the default program of opening a certain type of file programmatically?
提问by user26404
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.
我的程序在 MFC 中生成了一个可执行文件,我想将它用作打开.jpg
文件的默认程序。也就是说,每次我双击一个.jpg
文件,我的程序就会运行。
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
文件与我的程序相关联,例如HKEY_CLASSES_ROOT\.jpg\shell\open\command
(将其值设置为"myProgram.exe" "%1"
)和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!
该方法工作得很好,除非其他一些应用程序注册自己打开.jpg
文件。例如,我在我的电脑上安装了acdSee,所以每次我双击一个.jpg
文件时,它总是启动acdSee而不是我自己的程序。但是当我用我的程序注册一个全新类型的文件时,它可以在程序中打开。我不知道如何以编程方式将我的程序设置为已注册文件的默认打开程序。谁能帮我解决这个问题?非常感谢!
回答by Charlie
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:
执行此操作的更典型/标准方法是将“.jpg”键的默认值设置为更清楚地标识文件类型的名称,然后在那里设置各种相关操作。所以对于 jpg,你可以这样做:
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.
如果其他程序决定注册该类型,它们会将 HKCR.jpg 的默认值替换为其他值,例如 OtherProgram.Jpg。此时,您可以通过将值设置回 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:
如果您想向现有的注册类型添加一些额外的命令,您可以读取 .jpg 键的默认值来查找文件类型的名称。然后您可以打开该键并向现有操作集添加一个操作。例如,您可以添加以下内容:
HKCR\ExistingApp.JpegImage\shell\myopen\
@default = "Open with MyApp"
HKCR\ExistingApp.JpegImage\shell\myopen\command\
@default = "myApp.exe "%1""
回答by Factor Mystic
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.
请注意,通过向 HKCR 写入密钥,您实际上是在写入 HKLM\Software\Classes。这将需要管理权限。但是,您可以通过将密钥写入 HKCU\Classes\Root 来在用户上下文中对每个用户进行更改。
Also, user preferences in HKCU will override the system defaults in HKLM, which sounds like what your problem might be.
此外,HKCU 中的用户首选项将覆盖 HKLM 中的系统默认值,这听起来可能是您的问题。
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!
是时候开始阅读文档了!