开发者

InstalledInstances doesn't work

开发者 https://www.devze.com 2023-03-30 03:59 出处:网络
I have a project with a database and I have to create a setup file to run another computer. I try to setup but firstly, I need to know is there any SQL Server already installed on that computer. I sea

I have a project with a database and I have to create a setup file to run another computer. I try to setup but firstly, I need to know is there any SQL Server already installed on that computer. I searched some code about it and I found:

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

but everytime instances equal null everytime. But when I try to look myself on computer I find by hand. What's 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();
          }
       }
    }
}


You need to drop the initial \:

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

But when run on my 64-bit machine from 32-bit .Net executable, it doesn't actually report the installed instances. That's because they are only in the 64-bit view of registry. To get there from 32-bit process under .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");


I'm not sure if this is the only problem but I notice you are escaping the " and not the \ so you want an @ prefix on the string and double " instead of \" like so:

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

Using the @ formatted strings is easier IMHO or you could go back and replace every instance of \ in the target path with \\


From MSDN it seems you have to test for 32 vs 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 need to check what key is taken, because you might not pointing to the correct key, to know what actual key that you have do this:

string keyValue = registryKey.ToString();

if you found a different key that what you have been using which is: SOFTWARE\\Microsoft\\Microsoft SQL Server, then you should change the projects build, since the registries can be for 32 or 64, so specify which CPU, not "any CPU"

0

精彩评论

暂无评论...
验证码 换一张
取 消