Check for installed components

Asked

Viewed 758 times

4

In my project I have a built-in browser so that the user can access a particular site he needs. The browser works, only it has as requirements 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?

  • List installed programs and find the components you need?

  • @Omni this way I could find only the two components ? I am afraid to affect performance by the fact of enumerating everything that is installed... This could happen ?

  • It will have an impact by listing the installed programs, but any component you use to list the programs will have an impact. The worst case will be O(n) (where n and the number of installed programs).

  • Got it.. But anyway I’ll need to do it right? Because if not the application will not work the right way.

  • Correct. You can mitigate the problem and only check the first time you run your application (just like installers do when they check that all requirements are met) (of course you may have problems if you uninstall the components).

  • It would be great, you could give me an example of how I would do it ?

Show 1 more comment

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 installed programs for the defined prerequisites. If you find them all, todosEncontrados and true. Should any, todosEncontrados and false.

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

if (!Settings.Default.PrerequisitosCumpridos)
{
    var cumpridos = AnalisarPrerequitos();
    if (cumpridos)
    {
        Settings.Default.PrerequisitosCumpridos = true;
        Settings.Default.Save();
    }
    else
    {
        // informa o utilizador
    }
}
  • @Ommi as this method would be: var cumpridos = AnalisarPrerequitos();?

  • @Erikthiago would be the first part of the answer code within a method.

  • bool todosEncontrados = preRequisitosEncontrados.All(p => p.Value); this part uses Entity? Ta giving error in All(p=>p.Value)... Just like here: List<string> preRequisitos = preRequisitosEncontrados.Keys.ToList();, the ToList() is not being recognized

  • @Érikthiago Usa Linq (System.Linq).

  • 1

    SHOW! Worked right! Thanks! @Omni

3

Just an alternative to @Omni’s excellent response.

You have some ways to do this. One would be to list the installed programs and check whether or not the desired program exists. This way you search for the name, but makes it less performative, since you will have to check if you have the program in a list of programs. An example would be 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");
                        }
                    }
                }
            }

Another alternative, as shown by Omni no chat, you can check by Registration key.

 RegistryKey RK = Registry.CurrentUser.OpenSubKey("Software\\Macromedia\\FlashPlayera");
            if (RK != null)
            {
                Console.WriteLine("Flash Instalado");
            }
  • thanks for the reply! Omni’s reply worked right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.