如何确定与特定扩展名相关联的应用程序(例如.JPG),然后确定该应用程序的可执行文件所在的位置,以便可以通过调用System.Diagnostics.Process.Start(.. )。
我已经知道如何读写注册表了。注册表的布局使得更难以以标准方式确定哪些应用程序与扩展相关联,哪些显示名称以及可执行文件的位置。
答案 0 :(得分: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);
}
}
}
}
}
答案 1 :(得分:8)
像Anders所说 - 使用IQueryAssociations COM接口是个好主意。 这是一个sample from pinvoke.net
答案 2 :(得分:4)
@aku:别忘了HKEY_CLASSES_ROOT \ SystemFileAssociations \
不确定它们是否在.NET中公开,但是有COM接口(IQueryAssociations和朋友)处理这个问题,所以你不必在注册表中捣乱并希望在下一个Windows版本中不会改变
答案 3 :(得分:1)
HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ FileExts \
。 EXT \ OpenWithList键,用于“Open width ...”列表('a','b','c','d'等选项的字符串值)
。 EXT \ UserChoice键,用于“始终使用所选程序打开此类文件”('Progid'字符串值值)
所有值都是键,使用方法与上例中的 docName 相同。
答案 4 :(得分:0)
文件类型关联存储在Windows注册表中,因此您应该能够使用Microsoft.Win32.Registry class来读取以哪种文件格式注册的应用程序。
以下是两篇可能有帮助的文章: