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

前端之家收集整理的这篇文章主要介绍了.net – Windows:列出并启动与扩展名关联的应用程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何确定与特定扩展(例如.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}\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) as string;

                if (!string.IsNullOrEmpty(associatedCmd))
                {
                    Console.WriteLine("\"{0}\" command is associated with {1} extension",associatedCmd,ext);
                }
            }
        }
    }
}
原文链接:https://www.f2er.com/windows/371382.html

猜你在找的Windows相关文章