如何将 Windows 应用程序与特定文件类型关联,但与其他应用程序共享该关联?
如果我创建一个新应用程序,并与特定计算机上的 .xml 文件扩展名关联,当有人双击 .xml 文件时,它将启动我的应用程序并将该文件作为参数传递。但 Windows 似乎知道哪些其他文件能够处理该文件类型。这是如何设置的?
另外,当我将 Microsoft Word 文件另存为 .xml 文件,然后双击该文件时,它将启动 Microsoft Word,即使 .xml 文件类型与其他内容(例如 Internet Explorer)相关联。似乎可能有一个与 .xml 文件类型关联的存根,当调用该存根时,该存根会查看内容并启动适当的应用程序。
是否有 Windows API 或某种标准方法可以做到这一点?
我想要创建一个应用程序来完全执行 Word 正在执行的操作 - 即以 .xml 格式保存文件,但双击时启动我的应用程序而不是 Internet Explorer。
If I create a new app, and associate with, say, the .xml file extension on a particular computer, when someone double clicks the .xml file, it will launch my app and pass the file as parameter. But Windows seems to know what other files have the ability to work with that file type. How is that set up?
Also, when I save a Microsoft Word file as an .xml file, then later double-click on the file, it will launch Microsoft Word, even though the .xml file type is associated with something else, such as Internet Explorer. Seems like there may be a stub associated with .xml file type which when invoked looks at the content and launches the appropriate app.
Is there a Windows API, or some kind of a standard way to do that?
What I wanted to create an app to do exactly what Word is doing -- i.e. save file in the .xml format, but when double-clicked, launches the my app instead of Internet Explorer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
打开 Office .xml 文档的机制
对于保存在 XML 中且具有 .xml 扩展名的 Word 文档,微软实现了一个特殊的处理程序来在相应的应用程序中打开这些文件(此机制不仅用于 Word 文档,还用于 Excel 电子表格、InfoPath 表单)以及其他一些格式)。
如果您检查注册表,您将看到扩展名为 .xml 的文件的文件类型设置为
xmlfile
:下指定
打开此文件类型时执行的命令在“所以当 XML 文件时” 在资源管理器中双击,Windows 将启动 MSOXMLED.EXE。该应用程序现在正在查看 XML 文件并搜索 XML 处理指令。这个名为mso-application的处理指令可以指定一个ProgId:
如果找到该处理指令并且ProgId是支持的值之一,MSOXMLED.EXE将在注册表中搜索为该ProgId指定的打开命令。对于 Word.Document,实际上有另一个使用 Word.Document 的 CurVer 子项重定向到 Word.Document12(如果安装了 Office 2007),因此我们最终得到
:最后,MSOXMLED.EXE 将启动相应的 Office 应用程序或启动在下面指定的默认 XML 应用程序
您实际上可以通过从命令行调用 MSOXMLED.EXE 来尝试此操作:
如果您想实现相同的行为,则必须实现像 MSOXMLED.EXE 这样的处理程序在文件内部查找预定义的处理指令,然后将文档路由到适当的应用程序。
图标处理
上面我们了解了文档打开和编辑的处理方式。另一种机制负责根据 XML 文档内的处理指令显示特定图标: 图标处理程序。
图标处理程序是一种 Explorer Shell 扩展,是可以与某些文件类型关联的进程内 COM 对象。用于 XML 文件的文件在注册表中的“
此 GUID”下指定,它引用 MSOXEV.dll,与 MSOXMLEX.EXE 类似,它会检查 XML 文件中的 ProgId,然后提供正确的图标。
由于所有这些都是一个相当复杂的机制,如果你想走这条路,你应该仔细考虑。在我看来,注册一个新的唯一文件扩展名要简单得多。它也受到限制,因为它仅适用于允许您在文件头中包含一些自定义信息(如 ProgId)的文件类型。
Microsoft 不再使用此方法,而是使用文件扩展名来表示新的 OpenXML 格式(请参阅 为什么 Office“.xml”文件的行为与其他“.xml”文件不同?)。
Mechanism for opening Office .xml Documents
For Word documents saved in XML and having an .xml extensions Microsoft implemented a special handler to open these files in the corresponding application (This mechanism is not only used for Word documents, but also Excel spreadsheets, InfoPath forms and some other formats).
If you check the Registry you will see that the file type for files with a .xml extension is set to
xmlfile
:The command that is executed when this file type is opened is specified under
So when an XML file is double-clicked in Explorer, Windows will launch MSOXMLED.EXE. This application is now looking inside the XML file and searches for an XML processing instruction. This processing instruction named mso-application can specify a ProgId:
If this processing instruction is found and the ProgId is one of the supported values MSOXMLED.EXE searches the Registry for the open command specified for that ProgId. For Word.Document there is actually another redirect to Word.Document12 (if Office 2007 is installed) using the CurVer subkey of Word.Document, so we end up with:
So finally MSOXMLED.EXE will start the appropriate Office application or launch the default XML application which is specified under
You can actually try this out by calling MSOXMLED.EXE from the command line:
If you would like to implement the same behavior you would have to implement a handler like MSOXMLED.EXE which looks inside the file for a pre-defined processing instruction and then routes the document to the appropriate application.
Icon handling
Above we looked at the way how document opening and editing is handled. Another mechanism is responsible for displaying a specific icon depending on the processing instruction inside the XML document: an icon handler.
Icon handlers are a type of Explorer Shell extensions which are in-process COM objects which can be associated with certain file types. The one used for XML files is specified in the Registry under
This GUID is refering to the MSOXEV.dll which will - similarly to MSOXMLEX.EXE inspect the XML file for the ProgId and then provide the correct icon.
As all this is a rather complicated mechanism you should consider carefully if you want to go this way. In my opinion it is far simpler to register a new unique file extension. It is also limited as it will only work with file types that allow you to include some custom information (as the ProgId) in the header of file.
Microsoft does not use this method anymore and uses file extensions instead for their new OpenXML formats (see Why do Office ".xml" files behave differently from other ".xml" files?).
那么,所有文件关联都存储在注册表中。有一篇文章“了解 MS Windows 文件关联”可能会有所帮助。
除此之外,还有很多方法可以以编程方式创建文件关联。尽管大多数情况下这是在安装程序中完成的。例如,我最喜欢的安装系统 NSIS 有一个宏来处理这个问题: http://nsis.sourceforge.net/FileAssoc
但是,设置默认程序来打开某种文件类型也存在挑战。我不知道您计划使用什么语言或安装系统来执行此操作,但问题似乎是 在这里为 NSIS 回答。当然,Microsoft 也有关于使用 Windows Installer 执行此操作的方法的文档。但正如您可能猜到的那样,我更喜欢 NSIS。 ;)
Well, all file associations are stored in the registry. There's an article, "Understanding MS Windows File Associations" that may help.
Beyond that, there are many ways to create file associations programmatically. Though most often this is done in the installer. For example, my favorite installation system NSIS has a macro to handle this: http://nsis.sourceforge.net/FileAssoc
But then there's the challenge of setting a default program to open a certain file type. I don't know what language or install system you're planning on using to do this but it appears that question is answered here for NSIS. Surely Microsoft also has documentation for their way of doing this with Windows Installer. But as you can probably guess, I'm more of an NSIS guy. ;)
在 Windows 资源管理器中,单击“工具”->“文件夹选项”->“文件类型”。通过单击“新建”或“高级”,您可以创建/更改您自己的应用程序与各种文件类型的关联,这些文件类型在您右键单击该文件时将出现。
编辑:我不知道是否有一个重定向特定文件的存根。您可以检查注册表中的“我的电脑\HKEY_CLASSES_ROOT”并查看其中放置的内容。
In Windows Explorer click on Tools->Folder Options->File Types. By clicking on "New" or "Advanced" you can create/change your own application associations with various file types that will appear when you right click on the file.
EDIT: I don't know if there's a stub that redirects specific files. You can check My Computer\HKEY_CLASSES_ROOT in the registry and see what it's putting in there.