Example #1
0
        /// <summary>
        /// Load shell commands for applications registered for this extension
        /// </summary>
        /// <param name="extension"></param>
        private void LoadApplicationShellCommands(string extension)
        {
            // see if we've already loaded commands for this extension
            if (m_extensionApplications.GetValues(extension) != null)
            {
                return;
            }

            var apps = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            RegistryKey regKey = Registry.CurrentUser.OpenSubKey(string.Concat(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\", extension, @"\OpenWithList"));

            if ((regKey != null) && (regKey.ValueCount > 0))
            {
                foreach (string name in regKey.GetValueNames())
                {
                    string value;
                    if (name.Equals("MRUList"))
                    {
                        continue;
                    }

                    if (regKey.GetValueKind(name) != RegistryValueKind.String)
                    {
                        continue;
                    }

                    value = (string)regKey.GetValue(name, string.Empty);
                    if (value.Equals(string.Empty))
                    {
                        continue;
                    }

                    apps.Add(value);
                }
            }

            string[] subKeys = RegistryHelper.GetSubKeyList(Registry.ClassesRoot, string.Concat(extension, @"\OpenWithList"));
            apps.AddValues(subKeys);

            if (apps.Count == 0)
            {
                m_extensionApplications.Add(extension, string.Empty);
                return;
            }

            // look up shell command for each potential app
            foreach (string app in apps)
            {
                ShellCommand command;
                string       applicationName;
                string       applicationFullName = null;

                applicationName = app.ToLowerInvariant();
                if (applicationName.Equals(string.Empty))
                {
                    continue;
                }

                // duplicate?
                if ((m_extensionApplications[extension] != null) && m_extensionApplications[extension].Contains(applicationName))
                {
                    continue;
                }

                m_extensionApplications.Add(extension, applicationName);

                // check if commands for this app were already loaded
                if (m_applicationCommands.ContainsKey(applicationName))
                {
                    continue;
                }

                command = ShellCommands.ApplicationOpenCommand(applicationName);

                if (command == null)
                {
                    continue;
                }

                m_applicationCommands.Add(applicationName, command);

                if (ApplicationFullPaths.ContainsKey(applicationName))
                {
                    continue;
                }

                // search for app in registry
                applicationFullName = RegistryHelper.StringValueFromRegKey(Registry.LocalMachine, string.Concat(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\", applicationName), null);
                if (applicationFullName != null)
                {
                    ApplicationFullPaths.Add(applicationName, applicationFullName.ToLowerInvariant());
                    continue;
                }

                // look in command
                applicationFullName = command.GetApplication();
                if (applicationFullName != null)
                {
                    ApplicationFullPaths.Add(applicationName, applicationFullName.ToLowerInvariant());
                    continue;
                }
            }

            return;
        }
Example #2
0
        /// <summary>
        /// Load shell commands for progids registered for this extension
        /// </summary>
        /// <param name="extension"></param>
        private void LoadProgIdShellCommands(string extension)
        {
            // see if we've already loaded commands for this extension
            if (m_extensionProgIds[extension] != null)
            {
                return;
            }

            var progIds = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            // first search HKCU explorer registered file extensions
            var subKeys = RegistryHelper.GetValueNames(Registry.CurrentUser, string.Concat(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\", extension, @"\OpenWithProgIds"));

            progIds.AddValues(subKeys);

            // check HKCR registered file extensions
            subKeys = RegistryHelper.GetValueNames(Registry.ClassesRoot, string.Concat(extension, @"\OpenWithProgids"));
            progIds.AddValues(subKeys);

            if (progIds.Count == 0)
            {
                m_extensionProgIds.Add(extension, string.Empty);
                return;
            }

            // look up shell command for each potential progid
            foreach (string progId in progIds)
            {
                ShellCommand command;
                bool         foundApp = false;
                string       app      = null;

                m_extensionProgIds.Add(extension, progId);

                if (m_progIdCommands.ContainsKey(progId))
                {
                    // commands for this progId were already loaded
                    continue;
                }

                command = ShellCommands.ProgIdOpenCommand(progId);
                if (command == null)
                {
                    continue;
                }

                if (!m_progIdApplications.ContainsKey(progId))
                {
                    // find application for this progId
                    app = command.GetApplication();

                    if (app != null)
                    {
                        FileInfo fi = new FileInfo(app);
                        if (fi.Exists)
                        {
                            app      = fi.FullName.ToLowerInvariant();
                            foundApp = true;
                        }

                        if (!foundApp)
                        {
                            // search for app in registry
                            app = RegistryHelper.StringValueFromRegKey(Registry.LocalMachine, string.Concat(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\", app), null);
                            if (app == null)
                            {
                                // now do a brute force search on the hard disk
                                app = FileHelper.FindApplication(command.GetApplication(), true);
                                if (app == null)
                                {
                                    continue;
                                }
                            }
                            app      = app.ToLowerInvariant();
                            foundApp = true;
                        }
                    }

                    if (foundApp)
                    {
                        m_progIdApplications.Add(progId, app);
                    }
                }

                m_progIdCommands.Add(progId, command);
            }

            return;
        }