加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

.net – Windows:列出并启动与扩展名关联的应用程序

发布时间:2020-12-13 20:19:14 所属栏目:Windows 来源:网络整理
导读:如何确定与特定扩展(例如.JPG)相关联的应用程序,然后确定该应用程序的可执行文件所在的位置,以便可以通过调用System.Diagnostics.Process.Start(…)启动它. 我已经知道如何读写注册表.注册表的布局使得更难以标准方式确定哪些应用程序与扩展相关联,显示名称
如何确定与特定扩展(例如.JPG)相关联的应用程序,然后确定该应用程序的可执行文件所在的位置,以便可以通过调用System.Diagnostics.Process.Start(…)启动它.

我已经知道如何读写注册表.注册表的布局使得更难以标准方式确定哪些应用程序与扩展相关联,显示名称以及可执行文件所在的位置.

示例代码:
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}shellopencommand";

            // 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) as string;

                if (!string.IsNullOrEmpty(associatedCmd))
                {
                    Console.WriteLine(""{0}" command is associated with {1} extension",associatedCmd,ext);
                }
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读