Windows: List and Launch applications associated with an extension

前端 未结 5 1118
萌比男神i
萌比男神i 2020-11-29 04:11

How to determine the applications associated with a particular extension (e.g. .JPG) and then determine where the executable to that application is located so that it can be

相关标签:
5条回答
  • 2020-11-29 04:40

    Like Anders said - It's a good idea to use the IQueryAssociations COM interface. Here's a sample from pinvoke.net

    0 讨论(0)
  • 2020-11-29 04:42

    @aku: Don't forget HKEY_CLASSES_ROOT\SystemFileAssociations\

    Not sure if they are exposed in .NET, but there are COM interfaces (IQueryAssociations and friends) that deal with this so you don't have to muck around in the registry and hope stuff does not change in the next windows version

    0 讨论(0)
  • 2020-11-29 04:49

    Also HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\

    .EXT\OpenWithList key for the "Open width..." list ('a', 'b', 'c', 'd' etc string values for the choices)

    .EXT\UserChoice key for the "Always use the selected program to open this kind of file" ('Progid' string value value)

    All values are keys, used the same way as docName in the example above.

    0 讨论(0)
  • 2020-11-29 04:50

    Sample code:

    using System;
    using Microsoft.Win32;
    
    namespace GetAssociatedApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string extPathTemplate = @"HKEY_CLASSES_ROOT\{0}";
                const string cmdPathTemplate = @"HKEY_CLASSES_ROOT\{0}\shell\open\command";
    
                // 1. Find out document type name for .jpeg files
                const string ext = ".jpeg";
    
                var extPath = string.Format(extPathTemplate, ext);
    
                var docName = Registry.GetValue(extPath, string.Empty, string.Empty) as string;
                if (!string.IsNullOrEmpty(docName))
                {
                    // 2. Find out which command is associated with our extension
                    var associatedCmdPath = string.Format(cmdPathTemplate, docName);
                    var associatedCmd = 
                        Registry.GetValue(associatedCmdPath, string.Empty, string.Empty) as string;
    
                    if (!string.IsNullOrEmpty(associatedCmd))
                    {
                        Console.WriteLine("\"{0}\" command is associated with {1} extension", associatedCmd, ext);
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 04:58

    The file type associations are stored in the Windows registry, so you should be able to use the Microsoft.Win32.Registry class to read which application is registered for which file format.

    Here are two articles that might be helpful:

    • Reading and Writing the Registry in .NET
    • Windows Registry Using C#
    0 讨论(0)
提交回复
热议问题