Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to get path to the programs that associated with file extension, preferably through 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.

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?

I'm using C#.

Thanks.

like image 723
Alexander Avatar asked Jul 13 '11 13:07

Alexander


People also ask

How do you associate a program with an extension?

Locate the file extension you want to modify and highlight it. Click the Change program button. Select the program you want to associate, open the file extension, and click OK.

What is OpenWithList?

OpenWithList Verb When you right-click a file in Windows Explorer, you see the Open command. If more than one product is associated with an extension, you see an Open With submenu. You can register different applications to open an extension by setting the OpenWithList key for the file extension in HKEY_CLASSES_ROOT.


2 Answers

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);
}
like image 86
LarsTech Avatar answered Sep 28 '22 14:09

LarsTech


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

like image 45
Orhan Cinar Avatar answered Sep 28 '22 13:09

Orhan Cinar