The Win32Api GetComputerName has a simpler equivalent in the .NET world:
Dim compName as String = Environment.MachineName.ToString
This blog is a depository for things I have found regarding VB.NET and C#.NET development (and other things I may need to remember). This blog is primarily for my own reference, but if the information I found is useful to others, then that is great. If others want to contribute with comments or other information, then that is great too.
Monday, April 13, 2009
Wednesday, April 8, 2009
Getting the Version of an OleDB Provider
There may a much simpler way of determining the version of an OleDb provider (like OraOledb.Oracle.1) than I have shown below, but I have yet to find one.
The OleDbConnection object will tell you the version of the Server through its ServerVersion property:
Dim myConn As New OleDbConnection(g_gtech.GetConnectionString)
myConn.Open()
Dim versionStr as String = myConn.ServerVersion.ToString
myConn.Dispose()
But, I often want to the know the version of the Provider itself. The only way I have found to do this is to sift through the registry and read the ProductVersion off the provider DLL itself.
For example:
Dim versionStr as String = _
GetProviderVersion("OraOleDb.Oracle.1")
Here is the supporting code:
Public Function GetProviderVersion(ByVal providerName As String) As String
Try
Dim clsid As String = GetRegistryValue(Registry.LocalMachine, _
"SOFTWARE\Classes\" + providerName + "\clsid", "")
If clsid.Trim = "" Then Return "Not Installed"
Dim path As String = _
GetRegistryValue(Registry.LocalMachine, "SOFTWARE\Classes\CLSID\" + clsid + "\InprocServer32", "")
Dim Info As FileVersionInfo
Info = FileVersionInfo.GetVersionInfo(path)
Return Info.ProductVersion.ToString
Catch ex As Exception
Return "Unable to get Version"
End Try
End Function
Public Function GetRegistryValue(ByVal regKey As RegistryKey, _
ByVal subKey As String, ByVal valueName As String) As String
Dim value As String = ""
Dim registryKey As RegistryKey = regKey
Dim registrySubKey As RegistryKey
registrySubKey = registryKey.OpenSubKey(subKey)
If registrySubKey IsNot Nothing Then
Try
value = registrySubKey.GetValue(valueName).ToString
Catch ex As Exception
value = ""
End Try
registrySubKey.Close()
End If
Return value
End Function
If there is a simple or more elegant way to find the Provider version, please comment.
The OleDbConnection object will tell you the version of the Server through its ServerVersion property:
Dim myConn As New OleDbConnection(g_gtech.GetConnectionString)
myConn.Open()
Dim versionStr as String = myConn.ServerVersion.ToString
myConn.Dispose()
But, I often want to the know the version of the Provider itself. The only way I have found to do this is to sift through the registry and read the ProductVersion off the provider DLL itself.
For example:
Dim versionStr as String = _
GetProviderVersion("OraOleDb.Oracle.1")
Here is the supporting code:
Public Function GetProviderVersion(ByVal providerName As String) As String
Try
Dim clsid As String = GetRegistryValue(Registry.LocalMachine, _
"SOFTWARE\Classes\" + providerName + "\clsid", "")
If clsid.Trim = "" Then Return "Not Installed"
Dim path As String = _
GetRegistryValue(Registry.LocalMachine, "SOFTWARE\Classes\CLSID\" + clsid + "\InprocServer32", "")
Dim Info As FileVersionInfo
Info = FileVersionInfo.GetVersionInfo(path)
Return Info.ProductVersion.ToString
Catch ex As Exception
Return "Unable to get Version"
End Try
End Function
Public Function GetRegistryValue(ByVal regKey As RegistryKey, _
ByVal subKey As String, ByVal valueName As String) As String
Dim value As String = ""
Dim registryKey As RegistryKey = regKey
Dim registrySubKey As RegistryKey
registrySubKey = registryKey.OpenSubKey(subKey)
If registrySubKey IsNot Nothing Then
Try
value = registrySubKey.GetValue(valueName).ToString
Catch ex As Exception
value = ""
End Try
registrySubKey.Close()
End If
Return value
End Function
If there is a simple or more elegant way to find the Provider version, please comment.
Subscribe to:
Posts (Atom)