检查SQL Server是否已安装C#
我正在完成包含SQL Server数据库的C#中的应用程序。检查SQL Server是否已安装C#
如何检查用户是否安装了SQL Server 2012 Express Local DB?
是否可以通过x86,x64上的注册表进行检查?
基本上这个想法是,如果用户没有安装SQL Server,应用程序建议安装它。
由于我正在安装的安装程序没有SQL Server 2012 Express Local DB的依赖项。
谢谢。
您必须遍历卸载GUID并找到一个以关键字“Microsoft SQL Server 2012”开头的GUID。您可以通过控制面板>程序和功能>找到它并查看“显示名称”列。
// HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \微软\的Windows \ CurrentVersion \卸载{guidVariable} \ DisplayName的
..应该通配符匹配 “2012 *的Microsoft SQL Server”。
我没有确切的代码,但这应该让你开始。只需循环“Uninstall”键的所有子项,然后通过获取该值来查找“DisplayName”键。下面的“GUID”变量应该是你的迭代器,因为你不知道这个值。我相信你可以得到所有的“卸载”键的子键的GUID值列表。
string UninstallRegKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
Guid UninstallGuid = new Guid(GUID);
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(UninstallRegKeyPath, true))
{
if (key == null)
{
return;
}
try
{
string guidText = UninstallGuid.ToString("B");
RegistryKey child = key.OpenSubKey(guidText);
if (child != null)
{
string displayName = child.GetValue("DisplayName").ToString();
if (displayName.Contains("Microsoft SQL Server 2012"))
{
// implement logic when MSSQL 2012 is found
}
child.Close();
}
}
}
只是要谨慎。有32位安装和64位安装。 Wow6432Node包含我认为的32位程序,因此默认情况下安装在C:\Program Files (x86)\
中的所有内容(但可能在任何地方)。还有另外一个位置,我可以让你找到所有的64位程序,它们默认安装在C:\Program Files\
(并且可能安装在任何地方)。
编辑:
试试这个。您可能还想用“CurrentUser”替换“LocalMachine”,因为许多安装程序允许您为您的用户或所有用户配置它们。
using (RegistryKey root = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
{
string searchKey = @"Microsoft SQL Server 2012";
string subKeyName = "DisplayName";
foreach (string keyname in root.GetSubKeyNames())
{
//Console.WriteLine(keyname);
using (RegistryKey key = root.OpenSubKey(keyname))
{
try // in case "DisplayName doesn't exist
{
string displayName = key.GetValue(subKeyName).ToString();
if (displayName.StartsWith(searchKey))
Console.WriteLine("GUID: " + keyname + Environment.NewLine + displayName + Environment.NewLine);
}
catch
{
}
}
}
}
Console.ReadLine();
丑陋地狱,但最好的办法可能。尼斯 - 将不得不记住;) – TomTom
谢谢,会试试这个。 –
的[检查是否是通过C#安装在计算机上的SQL Server]可能的复制(http://stackoverflow.com/questions/2443001/check-if-sql-server-is-installed-on-a - 机器通过c-sharp) –
已经尝试过,没有工作。该应用程序是32位和OS 64位。 –
不是直接的答案,但是将存储的应用程序数据替换为SQLite会不会更好? – trailmax