Windows:拡張機能に関連付けられたアプリケーションの一覧表示と起動
質問
特定の拡張子(.JPGなど)に関連付けられているアプリケーションを特定し、そのアプリケーションの実行可能ファイルがどこにあるかを特定して、System.Diagnostics.Process.Start(.. 。)。
レジストリの読み取りと書き込みの方法はすでに知っています。レジストリのレイアウトにより、拡張機能に関連付けられているアプリケーション、表示名、実行可能ファイルの場所を標準的な方法で判断することが難しくなります。
解決
サンプルコード:
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);
}
}
}
}
}
他のヒント
アンダースが言ったように-IQueryAssociations COMインターフェイスを使用するのは良い考えです。 pinvoke.netのサンプル
@aku:HKEY_CLASSES_ROOT \ SystemFileAssociations \を忘れないでください
.NETで公開されているかどうかはわかりませんが、これを処理するCOMインターフェイス(IQueryAssociationsおよび友人)があるため、レジストリをいじくり回したり、次のWindowsバージョンで変更されないことを期待したりできます。
HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ FileExts \
" Open width ..."の。 EXT \ OpenWithListキーリスト(「a」、「b」、「c」、「d」などの選択肢の文字列値)
。 EXT \ UserChoiceキーは、「選択したプログラムを常に使用してこの種類のファイルを開く」ためのものです。 (「Progid」文字列値値)
すべての値はキーであり、上記の例の docName と同じ方法で使用されます。
ファイルタイプの関連付けはWindowsレジストリに保存されるため、 Microsoft.Win32.Registryクラスを使用して、どのアプリケーションがどのファイル形式に登録されているかを読み取ります。
役立つ2つの記事を次に示します。