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
}
}
List installed programs and find the components you need?
– Omni
@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 ?
– Érik Thiago
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).
– Omni
Got it.. But anyway I’ll need to do it right? Because if not the application will not work the right way.
– Érik Thiago
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).
– Omni
It would be great, you could give me an example of how I would do it ?
– Érik Thiago