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

发布于 2024-11-19 23:44:00 字数 351 浏览 5 评论 0原文

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

  1. “打开方式”菜单中显示的程序列表 item
  2. 按照推荐出现的程序列表 “打开方式...”对话框。

UPD:

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

我正在使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可是我不能没有你 2024-11-26 23:44:00

我写了一个小例程:

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;
  }

它被这样调用:

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

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);
}
不打扰别人 2024-11-26 23:44:00

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

系统文件关联

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

ぶ宁プ宁ぶ 2024-11-26 23:44:00

我通过LarsTech改进了方法。现在它返回程序的路径。

public List<string> RecommendedPrograms(string ext)
{
  //Search programs names:
  List<string> names = new List<string>();
  string baseKey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ext;
  string s;

  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)
        {
          s = rk.GetValue(c.ToString()).ToString();
          if (s.ToLower().Contains(".exe"))
            names.Add(s);
        }
      }
    }
  }

  if (names.Count == 0)
    return names;

  //Search paths:
  List<string> paths = new List<string>();
  baseKey = @"Software\Classes\Applications\{0}\shell\open\command";

  foreach (string name in names)
    using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(String.Format(baseKey, name)))
    {
      if (rk != null)
      {
        s = rk.GetValue("").ToString();
        s = s.Substring(1, s.IndexOf("\"", 2) - 1); //remove quotes
        paths.Add(s);
      }
    }

  if (paths.Count > 0)
    return paths;

  //Search pathes for Windows XP:
  foreach (string name in names)
    using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(String.Format(baseKey, name)))
    {
      if (rk != null)
      {
        s = rk.GetValue("").ToString();
        s = s.Substring(1, s.IndexOf("\"", 2) - 1); //remove quotes
        paths.Add(s);
      }
    }

  return paths;
}

I improved method by LarsTech. Now it returns paths to programs.

public List<string> RecommendedPrograms(string ext)
{
  //Search programs names:
  List<string> names = new List<string>();
  string baseKey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ext;
  string s;

  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)
        {
          s = rk.GetValue(c.ToString()).ToString();
          if (s.ToLower().Contains(".exe"))
            names.Add(s);
        }
      }
    }
  }

  if (names.Count == 0)
    return names;

  //Search paths:
  List<string> paths = new List<string>();
  baseKey = @"Software\Classes\Applications\{0}\shell\open\command";

  foreach (string name in names)
    using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(String.Format(baseKey, name)))
    {
      if (rk != null)
      {
        s = rk.GetValue("").ToString();
        s = s.Substring(1, s.IndexOf("\"", 2) - 1); //remove quotes
        paths.Add(s);
      }
    }

  if (paths.Count > 0)
    return paths;

  //Search pathes for Windows XP:
  foreach (string name in names)
    using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(String.Format(baseKey, name)))
    {
      if (rk != null)
      {
        s = rk.GetValue("").ToString();
        s = s.Substring(1, s.IndexOf("\"", 2) - 1); //remove quotes
        paths.Add(s);
      }
    }

  return paths;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文