windows 如何在 C# 中获取与文件扩展名关联的推荐程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6679385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:25:02  来源:igfitidea点击:

How to get recommended programs associated with file extension in C#

c#windowsfile-association

提问by Alexander

I want to get path to the programs that associated with file extension, preferably through Win32 API.

我想获得与文件扩展名关联的程序的路径,最好通过 Win32 API。

  1. List of programs that appears in "Open With" menu item
  2. List of programs that appears as recommended in "Open With..." dialog.
  1. 出现在“打开方式”菜单项中的程序列表
  2. 在“打开方式...”对话框中显示的程序列表。

UPD:

更新:

Assume that i have office11 and office12 installed on my machine, default program for .xls is office 11. If look at HKEY_CLASSES_ROOT\Excel.Sheet.8\shell\Open\command there is a path to office11 excel.exe, but when i right click on file i can choose office12 in Open With menu item. So where is this association stored?

假设我的机器上安装了 office11 和 office12,.xls 的默认程序是 office 11。如果查看 HKEY_CLASSES_ROOT\Excel.Sheet.8\shell\Open\command 有一个到 office11 excel.exe 的路径,但是当我右键单击文件,我可以在“打开方式”菜单项中选择 office12。那么这个关联存储在哪里呢?

I'm using C#.

我正在使用 C#。

Thanks.

谢谢。

回答by LarsTech

I wrote a small routine:

我写了一个小程序:

public IEnumerable<string> RecommendedPrograms(string ext)
{
  List<string> progs = new List<string>();

  string baseKey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ext;

  using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(baseKey + @"\OpenWithList"))
  {
    if (rk != null)
    {
      string mruList = (string)rk.GetValue("MRUList");
      if (mruList != null)
      {
        foreach (char c in mruList.ToString())
          progs.Add(rk.GetValue(c.ToString()).ToString());
      }
    }
  }

  using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(baseKey + @"\OpenWithProgids"))
  {
    if (rk != null)
    {
      foreach (string item in rk.GetValueNames())
        progs.Add(item);
    }
    //TO DO: Convert ProgID to ProgramName, etc.
  }

  return progs;
  }

which gets called like so:

它被这样调用:

foreach (string prog in RecommendedPrograms("vb"))
{
  MessageBox.Show(prog);
}

回答by Orhan Cinar

Ever wanted to programmatically associate a file type on the system with your application, but didn't like the idea of digging through the registry yourself? If so, then this article and code are right for you.

曾经想以编程方式将系统上的文件类型与您的应用程序相关联,但不喜欢自己挖掘注册表的想法吗?如果是这样,那么这篇文章和代码适合您。

System File Association

系统文件关联