Windows:列出和启动与扩展相关联的应用程序

Pau*_*uly 14 .net windows registry

如何确定与特定扩展相关联的应用程序(例如.JPG),然后确定该应用程序的可执行文件所在的位置,以便可以通过调用System.Diagnostics.Process.Start(...)来启动它.

我已经知道如何读写注册表了.注册表的布局使得更难以以标准方式确定哪些应用程序与扩展相关联,哪些显示名称以及它们的可执行文件所在的位置.

aku*_*aku 8

示例代码:

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);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 最好使用IQueryAssociations (7认同)

Bar*_*ndo 8

像Anders说的那样 - 使用IQueryAssociations COM接口是个好主意.这是来自pinvoke.net样本

  • 包含的链接用于AssocCreate.AssocQuery的链接是这样的:http://www.pinvoke.net/default.aspx/shlwapi.AssocQueryString (2认同)

And*_*ers 5

@aku:不要忘记 HKEY_CLASSES_ROOT\SystemFileAssociations\

不确定它们是否在 .NET 中公开,但是有处理此问题的 COM 接口(IQueryAssociations 和朋友),因此您不必在注册表中四处走动,并希望在下一个 Windows 版本中不会发生变化