Windows:列出和启动与扩展关联的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24954/
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 11:07:17  来源:igfitidea点击:

Windows: List and Launch applications associated with an extension

.netwindowsregistry

提问by Pauly

How to determine the applications associated with a particular extension (e.g. .JPG) and then determine where the executable to that application is located so that it can be launched via a call to say System.Diagnostics.Process.Start(...).

如何确定与特定扩展名(例如 .JPG)相关联的应用程序,然后确定该应用程序的可执行文件所在的位置,以便可以通过调用 System.Diagnostics.Process.Start(...) 来启动它。

I already know how to read and write to the registry. It is the layout of the registry that makes it harder to determine in a standard way what applications are associated with an extension, what are there display names, and where their executables are located.

我已经知道如何读取和写入注册表。注册表的布局使得以标准方式确定哪些应用程序与扩展相关联、显示名称有哪些以及它们的可执行文件位于何处变得更加困难。

采纳答案by aku

Sample code:

示例代码:

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

回答by Barakando

Like Anders said - It's a good idea to use the IQueryAssociations COM interface. Here's a sample from pinvoke.net

就像安德斯所说 - 使用 IQueryAssociations COM 接口是个好主意。这是来自 pinvoke.net示例

回答by Anders

@aku: Don't forget HKEY_CLASSES_ROOT\SystemFileAssociations\

@aku:不要忘记 HKEY_CLASSES_ROOT\SystemFileAssociations\

Not sure if they are exposed in .NET, but there are COM interfaces (IQueryAssociations and friends) that deal with this so you don't have to muck around in the registry and hope stuff does not change in the next windows version

不确定它们是否在 .NET 中公开,但是有处理此问题的 COM 接口(IQueryAssociations 和朋友),因此您不必在注册表中四处走动,并希望在下一个 Windows 版本中不会发生变化

回答by user362515

Also HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\

还有 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\

.EXT\OpenWithList key for the "Open width..." list ('a', 'b', 'c', 'd' etc string values for the choices)

. “开放宽度...”列表的EXT\OpenWithList 键(选项的“a”、“b”、“c”、“d”等字符串值)

.EXT\UserChoice key for the "Always use the selected program to open this kind of file" ('Progid' string value value)

. “始终使用所选程序打开此类文件”的EXT\UserChoice 键('Progid' 字符串值)

All values are keys, used the same way as docNamein the example above.

所有值都是键,使用方式与上面示例中的docName相同。

回答by Erik ?jebo

The file type associations are stored in the Windows registry, so you should be able to use the Microsoft.Win32.Registry classto read which application is registered for which file format.

文件类型关联存储在 Windows 注册表中,因此您应该能够使用Microsoft.Win32.Registry 类来读取哪个应用程序为哪种文件格式注册。

Here are two articles that might be helpful:

这里有两篇文章可能会有所帮助: