Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

windows - 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_ROOTExcel.Sheet.8shellOpencommand 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I wrote a small routine:

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

  string baseKey = @"SoftwareMicrosoftWindowsCurrentVersionExplorerFileExts." + 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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...