我需要使用vb代码在我的机器上安装我的SQL Server实例的完整路径的文件夹。
例如,我安装了实例MyComputer\MyInstanceName
。
我知道它在C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL
但是如何使用vb.net代码获取此路径?
由于
答案 0 :(得分:1)
这并不是那么明显,但你可以在注册表中找到你想要的所有内容。
基本上:
您需要检查HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL
子项以查找已安装的实例及其“内部”名称
使用该内部名称,您可以检查HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\(internal name)\Setup
节点以查找有关该实例的所有信息
在C#代码中(对于64位操作系统,64位版本的SQL Server),这将是:
// open the 64-bit view of the registry, if you're using a 64-bit OS
RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
// find the installed SQL Server instance names
RegistryKey key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL");
// loop over those instances
foreach (string sqlInstance in key.GetValueNames())
{
Console.WriteLine("SQL Server instance: {0}", sqlInstance);
// find the SQL Server internal name for the instance
string internalName = key.GetValue(sqlInstance).ToString();
Console.WriteLine("\tInternal instance name: {0}", internalName);
// using that internal name - find the "Setup" node in the registry
string instanceSetupNode = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup", internalName);
RegistryKey setupKey = baseKey.OpenSubKey(instanceSetupNode, false);
if (setupKey != null)
{
// in the "Setup" node, you have several interesting items, like
// * edition and version of that instance
// * base path for the instance itself, and for the data for that instance
string edition = setupKey.GetValue("Edition").ToString();
string pathToInstance = setupKey.GetValue("SQLBinRoot").ToString();
string version = setupKey.GetValue("Version").ToString();
Console.WriteLine("\tEdition : {0}", edition);
Console.WriteLine("\tVersion : {0}", version);
Console.WriteLine("\tPath to instance: {0}", pathToInstance);
}
}