2008-08-24 26 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 –

4

が@aku:HKEY_CLASSES_ROOT \ SystemFileAssociationsを忘れてはいけない。ここ

は役に立つかもしれません2件の記事があります\

.NETで公開されているかどうかは不明ですが、これを処理するCOMインターフェイス(IQueryAssociationsおよびfriends)がありますので、レジストリで動かす必要はなく、次のWindowsバージョンでは変更されません

8

Anders氏のように - IQueryAssociations COMインターフェイスを使用することをお勧めします。 は、ここでまたsample from pinvoke.net

+2

が含まれているリンクはAssocCreate用です。 AssocQueryへのリンクは次のとおりです。http://www.pinvoke.net/default.aspx/shlwapi.AssocQueryString – epotter

1

ます。HKEY_CURRENT_USER \ Software \ Microsoft \ Windowsの\ CurrentVersionの\ Explorerの\ FileExts \

です。 EXT \ OpenWithListの "Open width ..."リスト(選択肢の 'a'、 'b'、 'c'、 'd'などの文字列値)

(「PROGID」文字列値の値を)「常にこの種類のファイルを開くときは、選択したプログラムを使用する」

ためEXT \ UserChoiceキーのすべての値が鍵である、上記の例でDOCNAMEと同じように使用。

関連する問題