如何将 Windows 应用程序与特定文件类型相关联,但与其他应用程序共享该关联?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1569102/
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 associate a Windows application with a particular file type but share that association with other applications?
提问by zumalifeguard
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?
如果我创建一个新应用程序,并与特定计算机上的 .xml 文件扩展名关联,当有人双击 .xml 文件时,它将启动我的应用程序并将文件作为参数传递。但是 Windows 似乎知道哪些其他文件可以使用该文件类型。那是怎么设置的?
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.
此外,当我将 Microsoft Word 文件另存为 .xml 文件,然后双击该文件时,它会启动 Microsoft Word,即使 .xml 文件类型与其他内容(例如 Internet Explorer)相关联。似乎有一个与 .xml 文件类型相关的存根,它在被调用时会查看内容并启动适当的应用程序。
Is there a Windows API, or some kind of a standard way to do that?
是否有 Windows API,或者某种标准的方法来做到这一点?
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.
我想创建一个应用程序来完成 Word 正在做的事情——即以 .xml 格式保存文件,但是当双击时,启动我的应用程序而不是 Internet Explorer。
回答by Dirk Vollmar
Mechanism for opening Office .xml Documents
Office .xml 文档的打开机制
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).
对于以 XML 格式保存并具有 .xml 扩展名的 Word 文档,Microsoft 实施了一个特殊的处理程序来在相应的应用程序中打开这些文件(此机制不仅用于 Word 文档,还用于 Excel 电子表格、InfoPath 表单和一些其他格式)。
If you check the Registry you will see that the file type for files with a .xml extension is set to xmlfile
:
如果您检查注册表,您将看到扩展名为 .xml 的文件的文件类型设置为xmlfile
:
HKEY_CLASSES_ROOT\.xml (Default) = "xmlfile"
The command that is executed when this file type is opened is specified under
打开此文件类型时执行的命令在下面指定
HKEY_CLASSES_ROOT\xmlfile\shell\open\command = ""C:\Program Files\Common Files\Microsoft Shared\OFFICE12\MSOXMLED.EXE" /verb open "%1""
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-applicationcan specify a ProgId:
因此,当在资源管理器中双击 XML 文件时,Windows 将启动 MSOXMLED.EXE。此应用程序现在正在查看 XML 文件内部并搜索 XML 处理指令。这个名为mso-application 的处理指令可以指定一个 ProgId:
<?mso-application progid="Word.Document"?>
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.Documentthere is actually another redirect to Word.Document12(if Office 2007 is installed) using the CurVer subkey of Word.Document, so we end up with:
如果找到此处理指令并且 ProgId 是受支持的值之一,MSOXMLED.EXE 将在注册表中搜索为该 ProgId 指定的打开命令。对于Word.Document,实际上还有另一个重定向到Word.Document12(如果安装了 Office 2007)使用 Word.Document 的 CurVer 子项,所以我们最终得到:
HKEY_CLASSES_ROOT\Word.Document.12\shell\Open\command = ""C:\Program Files\Microsoft Office\Office12\WINWORD.EXE" /n /dde"
So finally MSOXMLED.EXE will start the appropriate Office application or launch the default XML application which is specified under
因此,最后 MSOXMLED.EXE 将启动适当的 Office 应用程序或启动在下指定的默认 XML 应用程序
HKEY_CLASSES_ROOT\XEV.GenericApp\shell\open\command
You can actually try this out by calling MSOXMLED.EXE from the command line:
您实际上可以通过从命令行调用 MSOXMLED.EXE 来尝试一下:
MSOXMLED.EXE /verb OPEN "SampleWordMLDocument.xml"
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.
如果您想实现相同的行为,则必须实现一个像 MSOXMLED.EXE 这样的处理程序,它会在文件内部查找预定义的处理指令,然后将文档路由到适当的应用程序。
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.
上面我们查看了文档打开和编辑的处理方式。另一种机制负责根据 XML 文档内的处理指令显示特定图标:图标处理程序。
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
图标处理程序是一种 Explorer Shell 扩展,它是可以与某些文件类型相关联的进程内 COM 对象。用于 XML 文件的文件在注册表中指定
HKEY_CLASSES_ROOT\xmlfile\ShellEx\IconHandler = "{AB968F1E-E20B-403A-9EB8-72EB0EB6797E}"
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.
此 GUID 指的是 MSOXEV.dll,它类似于 MSOXMLEX.EXE 检查 XML 文件中的 ProgId,然后提供正确的图标。
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.
由于这是一个相当复杂的机制,如果你想走这条路,你应该仔细考虑。在我看来,注册一个新的唯一文件扩展名要简单得多。它也受到限制,因为它仅适用于允许您在文件头中包含一些自定义信息(作为 ProgId)的文件类型。
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?).
Microsoft 不再使用这种方法,而是使用文件扩展名来代替其新的 OpenXML 格式(请参阅为什么 Office“.xml”文件的行为与其他“.xml”文件不同?)。
回答by Steve Wortham
Well, all file associations are stored in the registry. There's an article, "Understanding MS Windows File Associations" that may help.
好吧,所有文件关联都存储在注册表中。有一篇文章“了解 MS Windows 文件关联”可能会有所帮助。
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
除此之外,还有很多方法可以以编程方式创建文件关联。虽然大多数情况下这是在安装程序中完成的。例如,我最喜欢的安装系统 NSIS 有一个宏来处理这个: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. ;)
但是接下来是设置默认程序以打开某种文件类型的挑战。我不知道您打算使用哪种语言或安装系统来执行此操作,但似乎在此处为 NSIS 回答了该问题。当然,Microsoft 也有关于他们使用 Windows Installer 执行此操作的方法的文档。但正如您可能猜到的,我更像是一个 NSIS 人。;)
回答by Jacobs Data Solutions
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.
在 Windows 资源管理器中,单击工具->文件夹选项->文件类型。通过单击“新建”或“高级”,您可以创建/更改您自己的应用程序与各种文件类型的关联,这些文件类型将在您右键单击文件时出现。
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.
编辑:我不知道是否有重定向特定文件的存根。您可以检查注册表中的 My Computer\HKEY_CLASSES_ROOT 并查看它在那里放置的内容。