One way to check whether SQL Server Local DB is installed is through HKLM\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions
already quoted in @daniloloko's response, however, you need to specify for RegistryKey
whether the system is x86
or x64
.
We can do this through the Is64BitOperatingSystem
property of the class Environment to determine whether the system is 64bit or 32bit, then specify which record view should be used RegistryView.Registry64;
or RegistryView.Registry32;
on the RegistryView
object.
Follow the software that verifies that SQL Server Local BD is installed:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace SQLServerLocalDBInstalledExemplo
{
class Program
{
static void Main(string[] args)
{
RegistryView registryViewArchitecture;
if (Environment.Is64BitOperatingSystem)
registryViewArchitecture = RegistryView.Registry64;
else
registryViewArchitecture = RegistryView.Registry32;
using (RegistryKey registryKeyLocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryViewArchitecture))
{
RegistryKey registryKey = registryKeyLocalMachine.OpenSubKey(@"HKLM\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions", false);
if (registryKey != null)
Console.WriteLine("SQL Server Local DB instalado.");
else
Console.WriteLine("SQL Server Local DB não esta instalado.");
}
Console.ReadKey();
}
}
}
To manipulate the registry, you must add the namespace. Win32 , also see that it was necessary to use the OpenBaseKey
method of the RegistryKey
class, this method allows you to open a new RegistryKey
representing the requested key on the local machine with the specified view in the registryViewArchitecture
object.
Sources:
Check if SQL Server is installed on a machine through C #
SQL2012 LocalDB: how to check in c # if it is currently installed?