I'm currently using .NET 4.0 which provides a very clean solution for accessing the 64 bit registry from a 32 bit process. Otherwise, if you're using an earlier version of the Framework then you need to use P/Invoke and call the function RegOpenKeyEx
with the KEY_WOW64_64KEY flag from the WINAPI
as described here.
But onto the solution that I used.
private static bool DoesMseExist()
{
using (RegistryKey localMachineX64View =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry64))
{
using (RegistryKey rk = localMachineX64View.OpenSubKey(location))
{
foreach (string subKey in rk.GetSubKeyNames())
{
using (RegistryKey productKey = rk.OpenSubKey(subKey))
{
if (productKey != null)
{
if (Convert.ToString(productKey.GetValue("DisplayName"))
.Contains("Microsoft Security Client"))
{
return true;
}
}
}
}
}
}
return false;
}
I was writing a P/Invoke solution to this problem, but then I came across this. All you need to do is use RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
and you will overcome the registry redirection. It's a very easy to understand and readable approach compared to the P/Invoke solution, in my opinion.