开发者

Windows Shell: How to associate different xml-files with different apps?

开发者 https://www.devze.com 2023-02-23 11:14 出处:网络
I have many kinds of xml-files (all with extension .xml)开发者_JAVA技巧 with different root element name and namespace. I want to associate each type with a different application and also make it poss

I have many kinds of xml-files (all with extension .xml)开发者_JAVA技巧 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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消