Windows: List and Launch applications associated with an extension

前端 未结 5 1119
萌比男神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: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);
                    }
                }
            }
        }
    }
    

提交回复
热议问题