Windows Shell:如何将不同的 xml 文件与不同的应用程序关联?

发布于 2024-10-31 18:08:33 字数 107 浏览 1 评论 0原文

我有多种 xml 文件(全部带有扩展名 .xml),具有不同的根元素名称和命名空间。我想将每种类型与不同的应用程序相关联,并且还可以为每种类型提供不同的文件图标。这可以使用 C# .NET 来完成吗?

I have many kinds of xml-files (all with extension .xml) with different root element name and namespace. I want to associate each type with a different application and also make it possible to have different file-icons for each type. Can this be done using C# .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林迷了鹿 2024-11-07 18:08:33

处理此问题的唯一方法与 Visual Studio 处理 .sln 文件的方式类似,即 Visual Studio 版本选择器。该应用程序与 .sln 文件关联,并提供图标和最终进程来处理特定 .sln 文件。假设您已安装 Visual Studio,请查看注册表中的 HKEY_CLASSES_ROOT\VisualStudio.Launcher.sln 以了解它是如何完成的。

因此基本上您需要:

  1. 编写一个可以决定如何处理 .xml 文件的可执行文件
  2. 将进程注册为负责处理 .xml 文件的进程
  3. 将逻辑放入可执行文件中,或者在您的可执行文件使用的配置中,它决定在每个文件的基础上做什么。

对于图标,请查看子项 ShellEx\IconHandler。您将看到它指向(在安装了 Visual Studio 2010 的 x64 计算机上)HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{9A2B23E4-2A50-48DB-B3C3-F5EA12947CB8},它列在 InprocServer32 下 负责提供文件图标的 DLL,在本例中是 C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSFileHandler.dll。您需要实现一个类似的 DLL,它共享启动程序进程的配置/逻辑,以确定在每个文件的基础上显示什么图标。

通常的警告: 在托管代码中编写 shell 扩展一直是一个很大的“不”,因为 shell 扩展会加载到显示公共文件对话框的任何进程中。这可能会导致混乱,因为在 .net 4.0 之前,只能将一个版本的 CLR 加载到进程中,因此在执行此操作之前必须非常确定。鉴于 .net 4.0 支持 并行处理,这对您来说可能不是问题。

The only way to handle this is in a similar way to that which Visual Studio uses to handle .sln files which is the Visual Studio Version Selector. This application is the one that gets associated with .sln files and handles providing an icon and an eventual process to handle the specific .sln file. Assuming you have Visual Studio installed, take a look in the registry at HKEY_CLASSES_ROOT\VisualStudio.Launcher.sln to see how it's done.

So basically you need to:

  1. Write an executable that can decide what to do with .xml files
  2. Register the process as the one responsible for handling .xml files
  3. Place logic in your executable, or in configuration that your executable consumes, that decides what to do on a per file basis.

For icons, take a look at the subkey ShellEx\IconHandler. You'll see that it points to (on an x64 machine with Visual Studio 2010 installed) HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{9A2B23E4-2A50-48DB-B3C3-F5EA12947CB8} which lists under InprocServer32 a DLL that is responsible for providing icons for files, in this instance C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSFileHandler.dll. You'll need to implement a similar DLL that shares the configuration/logic of your launcher process to determine what icon to show on a per file basis.

The usual caveat: Writing shell extensions in managed code has always been a big "no no" because shell extensions get loaded into any process that shows the common file dialogs. This can cause merry chaos as, up until .net 4.0, only one version of the CLR can be loaded into a process, so you have to be very sure before doing this. Given that .net 4.0 supports in-process side by side, this may not be an issue for you.

青萝楚歌 2024-11-07 18:08:33

不。对于 Windows,XML 文件就是 XML 文件。操作系统不会向内部查看与其关联的命名空间;而是查看与它相关联的命名空间。它只是一个 XML 文件。

Windows 将文件类型与其扩展名相关联,因此所有 XML 文件都是 XML 文件。您可以亲自看到这一点:重命名一个完全不包含 XML 的普通 .txt 文件,然后在资源管理器中刷新其文件夹的视图。您将看到图标从文本文件图像更改为 XML 文件图像。

No. To Windows, an XML file is an XML file. The OS doesn't look inside to see what namespace is associated with it; it's just an XML file.

Windows associates file types with their extension, so again, all XML files are XML files. You can see this for yourself: rename a normal .txt file that contains absolutely no XML, and then refresh the view of it's folder in Explorer. You'll see the icon change from a text file image to an XML file image.

浴红衣 2024-11-07 18:08:33

如果没有自定义扩展或中间程序,您就无法做到这一点。

也许一种选择是拥有一个指定来处理 XML 文件的自定义应用程序。当这个程序生成时,它会使用 XML 标签之一计算出文件的“类型”,并相应地生成正确的进程。然而,您不太可能为不同的“类型”提供不同的图标。

There isn't a way that you can do this without having custom extensions or an intermediary program.

Maybe one option would be to have a custom applicaiton which is assigned to handle XML files. When this is program is spawned it works out what the "type" of the file is using one of the XML tags and spawns the correct process accordingly. It's unlikely, however that you can give different "types" different icons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文