InstalledInstances does not work

I have a project with a database, and I have to create an installation file to start another computer. I am trying to configure, but first, I need to know if there is any SQL Server already installed on this computer. I was looking for some code about this and I found:

RegistryKey rk = Registry.LocalMachine.OpenSubKey("\\SOFTWARE\\Microsoft\\Microsoft SQL Server"); String[] instances = (String[])rk.GetValue("InstalledInstances"); 

but every time the instances are zero every time. But when I try to look on the computer, I find it manually. What is the wrong code?

 RegistryKey rk = Registry.LocalMachine.OpenSubKey("\\SOFTWARE\\Microsoft\\Microsoft SQL Server"); String[] instances = (String[])rk.GetValue("InstalledInstances"); if (instances.Length > 0) { foreach (String element in instances) { if (element == "MSSQLSERVER") { DialogResult res = MessageBox.Show("are u sure to setup this file?", "UYARI", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\SQLEXPR.EXE"; Process p = new Process(); p.StartInfo.FileName = path; p.StartInfo.Arguments = "/qb INSTANCENAME=\"SQLEXPRESS\" INSTALLSQLDIR=\"C:\\Program Files\\Microsoft SQL Server\" INSTALLSQLSHAREDDIR=\"C:\\Program Files\\Microsoft SQL Server\" INSTALLSQLDATADIR=\"C:\\Program Files\\Microsoft SQL Server\" ADDLOCAL=\"All\" SQLAUTOSTART=1 SQLBROWSERAUTOSTART=0 SQLBROWSERACCOUNT=\"NT AUTHORITY\\SYSTEM\" SQLACCOUNT=\"NT AUTHORITY\\SYSTEM\" SECURITYMODE=SQL SAPWD=\"\" SQLCOLLATION=\"SQL_Latin1_General_Cp1_CS_AS\" DISABLENETWORKPROTOCOLS=0 ERRORREPORTING=1 ENABLERANU=0"; p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.Start(); p.WaitForExit(); CreateDB(); } else { this.Close(); } } } } 
+4
source share
3 answers

You need to drop the start \ :

 Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server"); 

But when running on my 64-bit computer from a 32-bit .Net executable, it actually does not report installed instances. This is because they are only in a 64-bit registry representation. To switch from a 32-bit process to .Net 4, you can use this code :

 var localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); var rk = localMachine.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server"); var instances = (String[])rk.GetValue("InstalledInstances"); 
+2
source

I'm not sure this is the only problem, but I notice that you are avoiding " , not \ , so you need the @ prefix for the string and double " instead of \" like this:

  p.StartInfo.Arguments = @"/qb INSTANCENAME=""SQ... rest of string ... "; 

Using formatted strings @ easier IMHO, or you can go back and replace each instance \ in the target path with \\


From MSDN it seems that you need to test 32 against 64

  try { // That works fine in Win32 but not in Win64 return Registry.LocalMachine.OpenSubKey("Software\\XXX\\YYY").GetValue("Path").ToString(); } catch (Exception) { // That works fine in Win64 but not in Win32 return Registry.LocalMachine.OpenSubKey("\\Software\\XXX\\YYY").GetValue("Path").ToString(); } 
+1
source

you need to check which key is taken, because you cannot point to the correct key to find out what actual key you are making:

 string keyValue = registryKey.ToString(); 

if you find another key that you used: SOFTWARE\\Microsoft\\Microsoft SQL Server , then you should change the project assembly, because the registries can be for 32 or 64, so indicate which processor, not "any processor"

0
source

All Articles