Check for installed components

4

In my project I have a built-in browser so the user can access a certain site that he needs. The browser works, except that it has requirements for the C ++ 2012 library and Flash.

Is there any way to find out if these components are installed on the operating system, without installing other components?

    
asked by anonymous 21.12.2015 / 12:40

2 answers

3

Using this answer as a basis:

// Dicionario que contem os prerequisitos necessarios. Por cada prerequisito,
// contem uma flag que indica se foi encontrado. No fim da pesquisa, se foram todos
// encontrados, retorna verdadeiro. Caso contrario retorna falso.
Dictionary<string, bool> preRequisitosEncontrados = new Dictionary<string, bool>
    {
        { "Adobe Flash Player", false },
        // ... outros componentes que necessite.
    };

List<string> preRequisitos = preRequisitosEncontrados.Keys.ToList();
const string caminhoRegistro = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey chave = Registry.LocalMachine.OpenSubKey(caminhoRegistro))
{
    if (chave != null)
    {
        foreach (string subChaves in chave.GetSubKeyNames())
        {
            using (RegistryKey subChave = chave.OpenSubKey(subChaves))
            {
                if (subChave == null)
                    continue;

                var name = subChave.GetValue("DisplayName") as string;
                if(string.IsNullOrWhiteSpace(name))
                    continue;

                var index = preRequisitos.FindIndex(s => name.Contains(s));
                if (index == -1)
                    continue;

                // Se encontrou um prerequisito, remove-o da lista e marco-o como encontrado.
                preRequisitosEncontrados[preRequisitos[index]] = true;
                preRequisitos.RemoveAt(index);

            }
        }
    }
}

bool todosEncontrados = preRequisitosEncontrados.All(p => p.Value);

This code searches the programs installed by the defined prerequisites. If you find them all, todosEncontrados is true. If something is missing, todosEncontrados and false.

You can run this code every time you start your application, and if it fails, for example, to show the user a message with missing components. Or you can run only the first time the application starts and save the search result in Settings :

if (!Settings.Default.PrerequisitosCumpridos)
{
    var cumpridos = AnalisarPrerequitos();
    if (cumpridos)
    {
        Settings.Default.PrerequisitosCumpridos = true;
        Settings.Default.Save();
    }
    else
    {
        // informa o utilizador
    }
}
    
21.12.2015 / 13:35
3

Just an alternative to the excellent response from @ Omni.

You have some ways to do this. One would be to list installed programs and check whether or not the program you want exists. That way you look for the name, but it makes it less performative, since you will have to check if you have the program in a list of programs. An example would look like this:

string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
            {
                foreach (string subkey_name in key.GetSubKeyNames())
                {
                    using (RegistryKey subkey = key.OpenSubKey(subkey_name))
                    {
                        if (Convert.ToString(subkey.GetValue("DisplayName")).Contains("Adobe Flash"))
                        {
                            Console.WriteLine("Possui Flash");
                        }
                        else
                        {
                            Console.WriteLine("NÃO Possui Flash");
                        }
                    }
                }
            }

Alternatively, as shown by Omni in chat , you can check for Registry Key .

 RegistryKey RK = Registry.CurrentUser.OpenSubKey("Software\Macromedia\FlashPlayera");
            if (RK != null)
            {
                Console.WriteLine("Flash Instalado");
            }
    
21.12.2015 / 13:38