I've got a project having a database and I must produce a setup file to operate another computer. I attempt to create but first of all, I have to know can there be any SQL Server already placed on that computer. I looked some code about this and that i found:

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

but each time instances equal null each time. However when I attempt to appear myself on computer I've found manually. What is the wrong this 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();
          }
       }
    }
}

I am unsure if this sounds like the only issue however i notice you're getting away the " and never the \ so you'd like an @ prefix around the string and double " rather than \" like so:

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

While using @ formatted strings is simpler IMHO or you might return and replace every demonstration of \ within the target path with \\


From MSDN it appears you need to test for 32 versus 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();
        }

You have to drop the first \:

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

However when operate on my 64-bit machine from 32-bit .Internet executable, it does not really report the installed instances. That's since they're only within the 64-bit look at registry. To obtain there from 32-bit process under .Internet 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");