如何确定与特定扩展(例如.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) as string;
if (!string.IsNullOrEmpty(associatedCmd))
{
Console.WriteLine("\"{0}\" command is associated with {1} extension",associatedCmd,ext);
}
}
}
}
}