Esempio n. 1
0
        public static ShellCommandRoot GetShellCommands(string path, bool isDirectory)
        {
            ShellCommandRoot result = new ShellCommandRoot();

            if (isDirectory)
            {
                RegistryKey appkey = Registry.ClassesRoot.OpenSubKey("Directory");
                if (appkey != null)
                {
                    ShellCommandSoftware soft = GetSoftware(appkey, "Directory");
                    result.Softwares.Add(soft);
                    result.Default = soft;
                }
            }
            else
            {
                string      ext    = Path.GetExtension(path);
                RegistryKey extkey = Registry.ClassesRoot.OpenSubKey(ext);
                if (extkey != null)
                {
                    result.ContentType   = extkey.GetValue("Content Type", string.Empty).ToString();
                    result.PerceivedType = extkey.GetValue("PerceivedType", string.Empty).ToString();

                    string      defaultApp = extkey.GetValue(string.Empty, string.Empty).ToString();
                    RegistryKey key        = extkey.OpenSubKey("OpenWithProgids");

                    List <string> subvalues = new List <string>();;
                    if (key != null)
                    {
                        subvalues.AddRange(key.GetValueNames());
                    }

                    if (!string.IsNullOrWhiteSpace(defaultApp))
                    {
                        if (subvalues.Contains(defaultApp))
                        {
                            subvalues.Remove(defaultApp);
                        }

                        subvalues.Insert(0, defaultApp);
                    }

                    foreach (string subkey in subvalues)
                    {
                        //Comme folder à vérifier comment simplifier
                        RegistryKey appkey = Registry.ClassesRoot.OpenSubKey(subkey);
                        if (appkey != null)
                        {
                            string application = appkey.GetValue(string.Empty, string.Empty).ToString();

                            ShellCommandSoftware soft = GetSoftware(appkey, subkey);
                            if (soft != null)
                            {
                                result.Softwares.Add(soft);
                                if (application == subkey)
                                {
                                    result.Default = soft;
                                }
                            }
                        }
                    }

                    subvalues.Clear();
                    key = extkey.OpenSubKey("OpenWithList");
                    if (key != null)
                    {
                        foreach (string subkey in key.GetSubKeyNames())
                        {
                            RegistryKey          appkey = Registry.ClassesRoot.OpenSubKey("Applications\\" + subkey);
                            ShellCommandSoftware soft   = GetSoftware(appkey, subkey);
                            if (soft != null)
                            {
                                result.Softwares.Add(soft);
                            }
                        }
                    }
                }
            }
            return(result);
        }
Esempio n. 2
0
        private static ShellCommandSoftware GetSoftware(RegistryKey appkey, string id)
        {
            ShellCommandSoftware soft = new ShellCommandSoftware
            {
                Id   = id,
                Name = id
            };

            RegistryKey subkey;

            string name = appkey.GetValue(string.Empty, string.Empty).ToString();

            if (string.IsNullOrEmpty(name))
            {
                name = ShellHelper.FileExtentionInfo(ShellHelper.AssocStr.FriendlyAppName, id);
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                name = appkey.GetValue("FriendlyTypeName", string.Empty).ToString();
                name = GetText(name);
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                name = appkey.GetValue("DisplayName", string.Empty).ToString();
                name = GetText(name);
            }
            if (!string.IsNullOrEmpty(name))
            {
                soft.Name = name;
            }

            string value = ShellHelper.FileExtentionInfo(ShellHelper.AssocStr.DefaultIcon, id);

            soft.Icon = GetIcon(value);
            if (soft.Icon == null)
            {
                value     = ShellHelper.FileExtentionInfo(ShellHelper.AssocStr.AppIconReference, id);
                soft.Icon = GetIcon(value);
            }

            if (soft.Icon == null)
            {
                subkey = appkey.OpenSubKey("DefaultIcon");
                if (subkey != null)
                {
                    value     = subkey.GetValue(string.Empty, string.Empty).ToString();
                    soft.Icon = GetIcon(value);
                }
            }

            string appid = string.Empty;

            subkey = appkey.OpenSubKey("Application");
            if (subkey != null)
            {
                appid = subkey.GetValue("AppUserModelID", string.Empty).ToString();

                if (soft.Icon == null)
                {
                    value     = subkey.GetValue(string.Empty, string.Empty).ToString();
                    soft.Icon = GetIcon(value);
                }

                if (string.IsNullOrEmpty(soft.Name))
                {
                    value     = subkey.GetValue("ApplicationName", string.Empty).ToString();
                    soft.Name = GetText(value);
                }
            }

            subkey = appkey.OpenSubKey("Shell");
            if (subkey != null)
            {
                string   defaultverb = subkey.GetValue(string.Empty, string.Empty).ToString();
                string[] appverbs    = subkey.GetSubKeyNames();
                if (appverbs.Contains("OPEN", StringComparer.OrdinalIgnoreCase) && appverbs[0].ToUpperInvariant() != "OPEN")
                {
                    string openOne = appverbs.First(T => T.ToUpperInvariant() == "OPEN");
                    int    pos     = Array.FindIndex(appverbs, T => T == openOne);
                    appverbs[pos] = appverbs[0];
                    appverbs[0]   = openOne;
                }
                foreach (string appverb in appverbs)
                {
                    RegistryKey      verbkey = subkey.OpenSubKey(appverb);
                    ShellCommandVerb verb    = GetVerb(verbkey, appverb, appid);
                    if (verb != null)
                    {
                        soft.Verbs.Add(verb);
                        if (defaultverb == appverb)
                        {
                            soft.Default = verb;
                        }
                    }
                }
            }
            if (string.IsNullOrEmpty(soft.Name))
            {
                string application = appkey.GetValue(string.Empty, string.Empty).ToString();
                if (!string.IsNullOrWhiteSpace(application))
                {
                    soft.Name = application;
                }
                else
                {
                    soft.Name = id;
                }
            }

            if (soft.Verbs.Count <= 0)
            {
                return(null);
            }

            if (soft.Icon == null)
            {
                appid = soft.Verbs.FirstOrDefault(T => T.Command.ToUpperInvariant().Contains(".EXE"))?.Command;
                if (!string.IsNullOrEmpty(appid))
                {
                    appid     = SplitCommandAndParameters(appid).Item1;
                    soft.Icon = GetIcon(appid + ",0");
                }
            }

            return(soft);
        }