示例#1
0
        public override IEnumerable <string> GetDefaultPrograms(string file)
        {
            string SearchExecutableInSameDirectory(string path)
            {
                if (Path.GetExtension(path) != "")
                {
                    return(path);
                }

                foreach (string ext in ExecutableFileExtensions)
                {
                    string newPath = Path.ChangeExtension(path, ext);
                    if (File.Exists(newPath))
                    {
                        return(newPath);
                    }
                }

                return(path);
            }

            string extension = Path.GetExtension(file);

            if (string.IsNullOrWhiteSpace(extension))
            {
                yield break;
            }

            string defaultProgramOnDblClick = Win32Native.AssocQueryString(Win32Native.AssocStr.Executable, extension);

            if (!string.IsNullOrWhiteSpace(defaultProgramOnDblClick) && File.Exists(defaultProgramOnDblClick))
            {
                yield return(Path.GetFullPath(defaultProgramOnDblClick));
            }

            string baseKey = $@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{extension}";

            using RegistryKey rk = Registry.CurrentUser.OpenSubKey($@"{baseKey}\OpenWithList");
            if (rk?.GetValue("MRUList") is not string mruList)
            {
                yield break;
            }

            foreach (char c in mruList)
            {
                string fullPath = SearchExecutableInSameDirectory(GetFullPath(rk.GetValue(c.ToString()).ToString()));
                if (fullPath == null)
                {
                    continue;
                }

                yield return(fullPath);
            }
        }