好吧,我會糊,以分享我的代碼:)(控制檯VB.Net應用程序)
Private Const INSTANCES As String = "SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS"
Private PathToReplace As String = "SOFTWARE\Microsoft\Microsoft SQL Server\{textToReplace}\Setup"
Sub Main()
'For now we must be sure we have only one instance of sql reporting service
Dim reportingInstances As List(Of String) = GetSQLReportingServicesInstances()
Dim InstanceName As String = GetKeyValue(INSTANCES, reportingInstances.Item(0))
Dim registryPathOfSqlReportingServices As String = PathToReplace.Replace("{textToReplace}", InstanceName)
Dim pathOfSqlReportingServices As String = GetKeyValue(registryPathOfSqlReportingServices, "SQLPath")
Console.WriteLine(pathOfSqlReportingServices)
Console.ReadLine()
End Sub
Public Function GetKeyValue(ByVal RegistryPath As String, ByVal key As String) As String
Dim localMachine As RegistryKey = GetRegistryParentKey()
Dim windowsNTKey As RegistryKey = localMachine.OpenSubKey(RegistryPath)
Return windowsNTKey.GetValue(key).ToString()
End Function
Public Function GetSQLReportingServicesInstances() As List(Of String)
Dim listOfInstances As New List(Of String)
Dim localMachine As RegistryKey = GetRegistryParentKey()
Dim InstancesKey As RegistryKey = localMachine.OpenSubKey(INSTANCES)
For Each InstanceKey As String In InstancesKey.GetValueNames
listOfInstances.Add(InstanceKey)
Next
Return listOfInstances
End Function
Public Function GetRegistryParentKey() As RegistryKey
Dim localMachine As RegistryKey = Nothing
If (Environment.Is64BitOperatingSystem) Then
localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64)
Else
localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32)
End If
Return localMachine
End Function
感謝您的反饋:)。我可以假設這個鍵是相對於SQL2008R2 ..但其他版本呢? – Rolando 2013-02-19 23:39:57
只需枚舉HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft SQL Server中以「MSRS」開頭並註冊所有版本的註冊表中的文件夾即可。 – Bryan 2013-02-19 23:47:39