簡體   English   中英

InstalledInstances不起作用

[英]InstalledInstances doesn't work

我有一個帶數據庫的項目,我必須創建一個安裝文件來運行另一台計算機。 我嘗試設置但首先,我需要知道是否已在該計算機上安裝任何SQL Server。 我搜索了一些關於它的代碼,我發現:

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

但每次實例每次都等於null。 但是,當我試圖在計算機上看到自己時,我會手工找到。 這段代碼有什么問題?

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();
          }
       }
    }
}

你需要刪除最初的\\

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

但是當從32位.Net可執行文件在我的64位機器上運行時,它實際上並不報告已安裝的實例。 那是因為它們只是在注冊表的64位視圖中。 要從.Net 4下的32位進程到達那里, 您可以使用以下代碼

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

我不知道如果這是唯一的問題,但我注意到你在逃避"而不是\\ ,所以你需要一個@的字符串和雙字頭" ,而不是\\"就像這樣:

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

使用@格式化的字符串更容易恕我直言,或者您可以返回並用\\\\替換目標路徑中的\\每個實例


MSDN看來,您必須測試32對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();
        }

你需要檢查一下是什么密鑰,因為你可能沒有指向正確的密鑰,知道你做了什么的實際密鑰:

string keyValue = registryKey.ToString();

如果你找到了一個不同的密鑰,你使用的是: SOFTWARE\\\\Microsoft\\\\Microsoft SQL Server ,那么你應該更改項目構建,因為注冊表可以是32或64,所以指定哪個CPU, 而不是“任何CPU“

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM