2008-08-24 91 views
14

如何確定與特定擴展名相關聯的應用程序(例如.JPG),然後確定該應用程序的可執行文件所在的位置,以便可以通過呼叫啓動System.Diagnostics.Process.Start(...)。Windows:與擴展關聯的列表和啓動應用程序

我已經知道如何讀取和寫入註冊表。這是註冊表的佈局,使得以標準方式難以確定哪些應用程序與擴展關聯,哪些顯示名稱以及可執行文件的位置。

回答

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

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

4

@aku:不要忘記HKEY_CLASSES_ROOT \ SystemFileAssociations \

不知道他們是否在.NET中公開,但有處理這個問題的COM接口(IQueryAssociations和朋友),所以你不必在註冊表中搞砸,並希望在下一個Windows版本中不會改變

8

像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

1

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

EXT \ OpenWithList鍵爲「打開寬度...」列表('a','b','c','d'等字符串值爲選項)

EXT \爲「始終使用選擇的程序打開這種文件的」(「的Progid」字符串值的值)UserChoice鍵

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

相關問題