.NET Framework版本检测指南

在将.NET应用程序部署到其他计算机之前,了解目标计算机是否安装了与应用程序要求相同的.NET Framework版本至关重要。通过互联网搜索,找到了一些关于这个问题的片段,并将其整合完善。

网络上的资料展示了如何检查.NET版本。这些资料指向了.NET Framework安装成功后在注册表中创建的特定位置。接下来,寻找了一个特定的模块文件,该文件通过注册表进行操作和控制。经过一些工作完善应用程序后,以下是操作步骤。

使用代码

在创建VB6项目后,需要添加一个模块文件(用于访问和控制注册表键值),并粘贴以下代码。这段代码并非编写,而是通过Google搜索得到的。以下是模块代码:

' 定义注册表值类型 Public Const REG_NONE As Long = 0 Public Const REG_SZ As Long = 1 Public Const REG_EXPAND_SZ As Long = 2 Public Const REG_BINARY As Long = 3 Public Const REG_DWORD As Long = 4 Public Const REG_LINK As Long = 6 Public Const REG_MULTI_SZ As Long = 7 Public Const REG_RESOURCE_LIST As Long = 8 ' 定义注册表部分 Public Const HKEY_CLASSES_ROOT = &H80000000 Public Const HKEY_CURRENT_USER = &H80000001 Public Const HKEY_LOCAL_MACHINE = &H80000002 Public Const HKEY_USERS = &H80000003 Public Const HKEY_PERFORMANCE_DATA = &H80000004 Public Const HKEY_CURRENT_CONFIG = &H80000005 Public Const HKEY_DYN_DATA = &H80000006 ' 定义Reg API调用返回的代码 Private Const ERROR_NONE = 0 Private Const ERROR_BADDB = 1 Private Const ERROR_BADKEY = 2 Private Const ERROR_CANTOPEN = 3 Private Const ERROR_CANTREAD = 4 Private Const ERROR_CANTWRITE = 5 Private Const ERROR_OUTOFMEMORY = 6 Private Const ERROR_INVALID_PARAMETER = 7 Private Const ERROR_ACCESS_DENIED = 8 Private Const ERROR_INVALID_PARAMETERS = 87 Private Const ERROR_NO_MORE_ITEMS = 259 ' 声明在模块中使用的注册表API函数 Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long

此例程允许从注册表的任何位置获取值,目前仅处理字符串、双字和二进制值。二进制值以十六进制字符串形式返回。

示例:

Text1.Text = ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultUserName")

公共函数ReadRegistry如下:

Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String) As String Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String, td As Double On Error Resume Next lResult = RegOpenKey(Group, Section, lKeyValue) sValue = Space$(2048) lValueLength = Len(sValue) lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue, lValueLength) If (lResult = 0) And (Err.Number = 0) Then If lDataTypeValue = REG_DWORD Then td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1))) sValue = Format$(td, "000") End If If lDataTypeValue = REG_BINARY Then ' 返回二进制字段作为十六进制字符串(每字节2个字符) TStr2 = "" For i = 1 To lValueLength TStr1 = Hex(Asc(Mid(sValue, i, 1))) If Len(TStr1) = 1 Then TStr1 = "0" & TStr1 TStr2 = TStr2 + TStr1 Next sValue = TStr2 Else sValue = Left$(sValue, lValueLength - 1) End If Else sValue = "Not Found" End If lResult = RegCloseKey(lKeyValue) ReadRegistry = sValue End Function

然后,返回到项目的主窗体并使用此模块的工作函数。如果想检查.NET v2.0的安装情况,请使用此函数的返回值。

VB.NET ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727", "Install")

如果想检查.NET v3.0的安装情况,请使用此函数的返回值。

VB.NET ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0", "Install")

如果想检查.NET v3.5的安装情况,请使用此函数的返回值。

VB.NET ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5", "Install")

如果想检查.NET v4.0的安装情况,请使用此函数的返回值。

VB.NET ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client", "Install")
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485