【发布时间】:2008-08-24 10:05:38
【问题描述】:
如何确定与特定扩展名(例如 .JPG)关联的应用程序,然后确定该应用程序的可执行文件所在的位置,以便可以通过调用 System.Diagnostics.Process.Start(.. .).
我已经知道如何读写注册表。正是注册表的布局使得以标准方式确定哪些应用程序与扩展相关联、显示名称是什么以及它们的可执行文件位于何处变得更加困难。
【问题讨论】:
如何确定与特定扩展名(例如 .JPG)关联的应用程序,然后确定该应用程序的可执行文件所在的位置,以便可以通过调用 System.Diagnostics.Process.Start(.. .).
我已经知道如何读写注册表。正是注册表的布局使得以标准方式确定哪些应用程序与扩展相关联、显示名称是什么以及它们的可执行文件位于何处变得更加困难。
【问题讨论】:
正如 Anders 所说 - 使用 IQueryAssociations COM 接口是个好主意。 这是sample from pinvoke.net
【讨论】:
@aku:不要忘记 HKEY_CLASSES_ROOT\SystemFileAssociations\
不确定它们是否在 .NET 中公开,但有处理此问题的 COM 接口(IQueryAssociations 和朋友),因此您不必在注册表中乱搞,并希望在下一个 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);
}
}
}
}
}
【讨论】:
还有 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\
.EXT\OpenWithList 键用于“打开宽度...”列表('a'、'b'、'c'、'd' 等字符串值用于选择)
.EXT\UserChoice 键为“始终使用所选程序打开此类文件”('Progid' 字符串值)
所有值都是键,使用方式与上例中的 docName 相同。
【讨论】:
文件类型关联存储在 Windows 注册表中,因此您应该能够使用 Microsoft.Win32.Registry class 来读取哪个应用程序注册了哪个文件格式。
这里有两篇文章可能会有所帮助:
【讨论】: