好的,我将粘贴我的代码以共享它:)(控制台 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