以编程方式检测计算机上的JDK安装

时间:2017-12-19 11:27:03

标签: vb.net

这是我检测机器上JDK安装功能的代码。 执行代码提供了处理的异常: - enter image description here

第164行是: - If ndpKey Is Nothing AndAlso ndpKey.GetValue("CurrentVersion") Is Nothing Then

Private Function HAS_JDK() As Boolean
    Try
        Const subkeyjava As String = "SOFTWARE\JavaSoft\Java Runtime Environment"

        Using ndpKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkeyjava)
            If ndpKey Is Nothing AndAlso ndpKey.GetValue("CurrentVersion") Is Nothing Then
                Close()
                Return False
            Else
                Return True
            End If
        End Using

    Catch e As NullReferenceException
        MessageBox.Show("System Error Occured While Checking JDK !! ", "BLA BLA BLA - Error Occured !!")
        MessageBox.Show(e.ToString())
        Close()
        Return -1
    End Try
End Function

1 个答案:

答案 0 :(得分:3)

你的逻辑有点不正确。我认为你所追求的是OrElse

If ndpKey Is Nothing OrElse ndpKey.GetValue("CurrentVersion") Is Nothing Then

这会检查 ndpKey ndpKey.GetValue("CurrentVersion")是否属实。如果第一次检查成功,它将不会继续第二次,因此也不例外。

相关问题