从字符串错误转换

时间:2013-04-23 11:05:33

标签: vb.net

你好我试图在其中一个语句为false时显示错误,但是我收到错误:“从字符串”False \ example.exe 转换为< / strong>'布尔值'无效。

If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EXAMPLE", "InstallLocation", "") <> "" And
        My.Computer.FileSystem.FileExists(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EXAMPLE", "InstallLocation", "ERROR")) & "\example.exe" Then
    Else
        System.Threading.Thread.Sleep(1000)
        example_error.ShowDialog()
    End If

编辑:

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ExecutablePath = IO.Path.Combine(CStr(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MYAPP", "InstallLocation", "ERROR")), "myapp.exe")
    If Not String.IsNullOrEmpty(CStr(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MYAPP", "InstallLocation", ""))) _
        AndAlso My.Computer.FileSystem.FileExists(ExecutablePath) Then
    Else
        System.Threading.Thread.Sleep(1000)
        GUI_Error.ShowDialog()
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

您的第二个条件不正确,因为您想要使用文件名连接路径并使用FileSystem.Exists检查文件是否存在,但文件名不在方法调用内。您应该使用Path.Combine

而不是连接字符串来创建路径
Dim filePath = Path.Combine(CStr(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EXAMPLE", "InstallLocation", "ERROR")),
                            "example.exe")
If Not String.IsNullOrEmpty(CStr(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EXAMPLE", "InstallLocation", ""))) _
    AndAlso File.Exists(filePath) Then
Else
    System.Threading.Thread.Sleep(1000)
    example_error.ShowDialog()
End If