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

问题描述 投票:-1回答:1

这是我在一台机器上检测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
vb.net
1个回答
3
投票

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

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

这检查ndpKeyndpKey.GetValue("CurrentVersion")是否为空。如果第一次检查成功,它将不会继续第二次,因此没有例外。

© www.soinside.com 2019 - 2024. All rights reserved.