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

问题描述:

如何确定与特定扩展名相关联的应用程序(例如.JPG),然后确定该应用程序的可执行文件所在的位置,以便可以通过呼叫启动System.Diagnostics.Process.Start(...)。Windows:与扩展关联的列表和启动应用程序

我已经知道如何读取和写入注册表。这是注册表的布局,使得以标准方式难以确定哪些应用程序与扩展关联,哪些显示名称以及可执行文件的位置。

示例代码:

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); 
       } 
      } 
     } 
    } 
} 
+7

更好地使用IQueryAssociations – 2009-10-02 02:12:52

文件类型关联存储在Windows注册表中,所以您应该能够使用Microsoft.Win32.Registry class来读取哪个应用程序注册了哪种文件格式。

这里有两篇文章可能会有所帮助:

@aku:不要忘记HKEY_CLASSES_ROOT \ SystemFileAssociations \

不知道他们是否在.NET中公开,但有处理这个问题的COM接口(IQueryAssociations和朋友),所以你不必在注册表中搞砸,并希望在下一个Windows版本中不会改变

像Anders说的那样 - 使用IQueryAssociations COM接口是个好主意。 这里有一个sample from pinvoke.net

+2

包含的链接用于AssocCreate。 AssocQuery的链接是这样的:http://www.pinvoke.net/default.aspx/shlwapi.AssocQueryString – epotter 2009-07-13 14:41:28

而且HKEY_CURRENT_USER \软件\微软\的Windows \ CurrentVersion \ Explorer中\ FileExts \

EXT \ OpenWithList键为“打开宽度...”列表('a','b','c','d'等字符串值为选项)

EXT \为“始终使用选择的程序打开这种文件的”(“的Progid”字符串值的值)UserChoice键

所有值都是键,所用的相同的方式DOCNAME在上述的例子。