SQL2012 LocalDB: how to check in c# if it is currently installed?

Viewed 11217

How to check in c# code if LocalDB currently installed? also, how to check if SQLNCLI11 presents in system?

4 Answers

I'm using the answer to this question to check for the existence of sqllocaldb.exe

Like so:

public static bool IsLocalDBInstalled()
{
    return ExistsOnPath("SqlLocalDB.exe"); ;
}

public static bool ExistsOnPath(string fileName)
{
    return GetFullPath(fileName) != null;
}

public static string GetFullPath(string fileName)
{
    if (File.Exists(fileName))
        return Path.GetFullPath(fileName);

    var values = Environment.GetEnvironmentVariable("PATH");
    foreach (var path in values.Split(';'))
    {
        var fullPath = Path.Combine(path, fileName);
        if (File.Exists(fullPath))
            return fullPath;
    }
    return null;
}

I found this nuget package that wraps up working with SQLLocalDB Has the following command

SqlLocalDbApi.IsLocalDBInstalled()
Related