using DbAPI.Connections;
using Microsoft.Win32;
using System.ComponentModel;
using System.Globalization;
namespace DbAPIUI
{
public class ConnectionDetailsEx: ConnectionDetails
{
[PasswordPropertyText(true)]
public override string DbUserPassword { get; set; } = "DTSSealBeachHQ";
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
public override string DbFolderPath { get; set; } = "";
[Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
public override string AttachDbsBatPath { get; set; } = "";
///
/// gets the path to SqlServer
///
///
public static string GetSqlServerLocalDbPath()
{
var highestVersionInstalledPath = string.Empty;
var rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var sk1 = rk.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server Local DB\\Installed Versions");
if (sk1 == null) return string.Empty;
var maxProductVersion = 0.0;
foreach (var productSubKeyName in sk1.GetSubKeyNames())
{
if (!double.TryParse(productSubKeyName, NumberStyles.Float, CultureInfo.InvariantCulture, out var thisVersion)) continue;
if (thisVersion < maxProductVersion) continue;
maxProductVersion = thisVersion;
var newKey = sk1.OpenSubKey(productSubKeyName);
if (newKey == null) continue;
var val = newKey.GetValue("InstanceAPIPath", -1, RegistryValueOptions.None).ToString();
if (val == "-1" || !val.EndsWith("SqlUserInstance.dll")) continue;
highestVersionInstalledPath = val.Substring(0, val.Length - "SqlUserInstance.dll".Length);
}
return highestVersionInstalledPath.Replace("LocalDB", "Tools");
}
}
}