与应用程序的文件类型关联 (C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/222561/
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
Filetype association with application (C#)
提问by Matías
I have a few questions related:
我有几个相关的问题:
1) Is possible to make my program change filetype association but only when is running? Do you see anything wrong with this behavior?
1) 是否可以让我的程序更改文件类型关联,但只能在运行时更改?你觉得这种行为有什么问题吗?
2) The other option that I'm seeing is to let users decide to open with my application or restore default association ... something like: "capture all .lala files" or "restore .lala association". How can I do this? What do you think that is the best approach?
2)我看到的另一个选项是让用户决定使用我的应用程序打开或恢复默认关联……例如:“捕获所有 .lala 文件”或“恢复 .lala 关联”。我怎样才能做到这一点?你认为最好的方法是什么?
采纳答案by Tomalak
Regarding file associations, I've wrote an answer earlier that at least covers the "How".
关于文件关联,我之前写过一个答案,至少涵盖了 "How"。
This should also point you to the right direction how to handle backup and restore. With direct registry access through c#, there will be no need to use .reg files, so you are free to back up the previous value however you like in your app, and also restore it from there.
这也应该为您指明如何处理备份和恢复的正确方向。通过通过 c# 直接访问注册表,将不需要使用 .reg 文件,因此您可以随意备份应用程序中您喜欢的以前的值,也可以从那里恢复它。
The key question here is: Shouldyou change file associations randomly? At least asking the user up-front would obviously be necessary (as you also indicated).
这里的关键问题是:是否应该随机更改文件关联?至少提前询问用户显然是必要的(正如您所指出的)。
Furthermore, Vista users with UAC enabled, or non-privileged users of other Windows versions may not have the required permission to change global file associations. The (un)installation procedure of your program may be the only place where this can succeed.
此外,启用了 UAC 的 Vista 用户或其他 Windows 版本的非特权用户可能没有更改全局文件关联所需的权限。程序的(卸载)安装过程可能是唯一可以成功的地方。
EDIT
编辑
As Franci Penov indicated in his answer, there isa way to change local file associations on a per-user basis, even for non-admins (that's why I spoke of "global associations" in the previous paragraph). He also mentioned mentioned why going there is not overly advisable.
作为弗郎佩诺夫在他的答复中指出,还有就是改变对每个用户进行本地文件关联,即使对非管理员的方式(这就是为什么我说在上段的“全球协会”的)。他还提到为什么去那里不太可取。
回答by Mike Marshall
It can probably be done but I think it would end up being cumbersome. All file type associations are stored in the registry so you would have to write/revert registry entries every time your app starts and stops. Also, depending on how frequently you do it the new associations may not be picked up in Windows explorer immediately.
它可能可以完成,但我认为它最终会很麻烦。所有文件类型关联都存储在注册表中,因此每次应用启动和停止时,您都必须写入/恢复注册表项。此外,根据您执行此操作的频率,可能不会立即在 Windows 资源管理器中获取新关联。
The associations are listed under HKEY_CLASSES_ROOT in the registry and can be mapped a whole myriad of ways (mime types/progIDs/etc).
关联列在注册表中的 HKEY_CLASSES_ROOT 下,可以通过多种方式进行映射(mime 类型/progIDs/等)。
Many apps I have seen ask if you want to associate certain file types with the application during install time, and give you the ability to opt-out and leave the current settings.
我见过的许多应用程序询问您是否希望在安装期间将某些文件类型与应用程序相关联,并让您能够选择退出并保留当前设置。
I don't think I'd recommend "on the fly" file type associations
我不认为我会推荐“动态”文件类型关联
回答by Stephen Wrighton
1) you get to define the file types that are in the Open Dialog file type droplist. Outside of that, it's possibleto change the filetype default on application open, and then replace during application close, as file type association are just a registry setting.
1) 您可以定义打开对话框文件类型下拉列表中的文件类型。除此之外,可以在应用程序打开时更改文件类型默认值,然后在应用程序关闭期间进行替换,因为文件类型关联只是注册表设置。
As for wrong, I wouldn't. First reason is that it's not the standard behavior of applications. The second reason is that if your application or PC exits unexpectedly, you run the risk of not returning the association to it's original setting.
至于错误,我不会。第一个原因是它不是应用程序的标准行为。第二个原因是,如果您的应用程序或 PC 意外退出,您将面临无法将关联恢复为其原始设置的风险。
2) Windows by default allows user to choose these options utilizing the right-click and the "open with" command.
2) 默认情况下,Windows 允许用户使用右键单击和“打开方式”命令来选择这些选项。
回答by Franci Penov
You can implement an "on the fly" file association change by associating a small executable with that file extension that upon start will check if your main application is running and pass the file name to it or if it's not running it'll invoke the "regular" associated application.
您可以通过将一个小型可执行文件与该文件扩展名相关联来实现“动态”文件关联更改,该文件扩展名在启动时将检查您的主应用程序是否正在运行并将文件名传递给它,或者如果它没有运行,它将调用“常规”相关应用程序。
The main advantage of this approach is that you need to muck with the registry only once.
这种方法的主要优点是您只需要修改注册表一次。
The main drawbacks of this approach are:
这种方法的主要缺点是:
- you need a helper process
- the application that "owns" these file extensions can detect the change and complain to the user, prompting "repair" thus getting you out of the picture.
- 你需要一个辅助进程
- “拥有”这些文件扩展名的应用程序可以检测到更改并向用户投诉,提示“修复”,从而使您摆脱困境。
Alternatively, you could change the file association upon your main program start. This will work even for non-admin users. while file associations are stored in HKEY_CLASSES_ROOT
, there's a small trick - HKCR is actually a map of both HKEY_LOCAL_MACHINE\SOFTWARE\Classes
and HKEY_CURRENT_USER\SOFTWARE\Classes
. Thus, you can temporarily register the file extension for the current user in HKCU and "shadow" the original association from HKLM.
或者,您可以在主程序启动时更改文件关联。这甚至适用于非管理员用户。虽然文件关联存储在 中HKEY_CLASSES_ROOT
,但有一个小技巧 - HKCR 实际上是HKEY_LOCAL_MACHINE\SOFTWARE\Classes
和的映射HKEY_CURRENT_USER\SOFTWARE\Classes
。因此,您可以在 HKCU 中临时注册当前用户的文件扩展名,并从 HKLM 中“隐藏”原始关联。
Of course, I would advise against this approach though, as it takes just one crash in your application to make that association permanent and since very few applications know how to deal with file associations in HKCU, chances are it'll be an unrecoverable situation for the original application.
当然,我建议不要使用这种方法,因为只需在您的应用程序中发生一次崩溃即可使该关联永久化,而且由于很少有应用程序知道如何处理 HKCU 中的文件关联,因此对于原始应用程序。