none
Weird Things Happen with These Lines of Codes RRS feed

  • 问题

  • Private Sub ControlLoad_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Lockheed Martin\Prepar3D v4", "SetupPath", Nothing) Is Nothing Then
                Form1.P4DInstalled = 0
                MessageBox.Show("You did not install any supported flight simulator.(Now supporting Lockheed Martin Prepar3D v4) Application will now exit.", "FS Not Found", MessageBoxButtons.OK)
                    Application.Exit()
            Else
                Form1.P4DInstalled = 1
                Form1.P4DDirectory = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Lockheed Martin\Prepar3D v4", "SetupPath", Nothing)
                Me.Visible = False
            End If
    End Sub

    These lines of codes I added in a user control are giving me trouble right now, what I expect it to do is to see if "HKEY_LOCAL_MACHINE\SOFTWARE\Lockheed Martin\Prepar3D v4\SetupPath" exists in registry, if it does, read the value then make the user control itself invisible, if not show a message box and close the application. However, if I use these lines, they don't seem to really read the registry and the message box just pops up and the app exits(The value it's supposed to read exists in my registry)More interestingly, whenever I click compile in Visual Studio, the message box also pops up and when I click OK, Visual Studio just closes for me somehow. It seems that these lines are taking effect on Visual Studio when compiling the app. I'm very confused right now but I can't find the problem in these codes. Could anyone tell me if I made a mistake in these lines or if there is something else that could have caused this? I will appreciate your help!



    2019年4月21日 22:29

全部回复

  • Hi,

    试着将你编译的目标平台设为Any CPU, 操作系统可以不同地虚拟化32位和64位注册表:

    https://docs.microsoft.com/zh-cn/windows/desktop/SysInfo/registry-virtualization

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年4月22日 2:02
  • 您好,

    我看了一下这一项的设置,但是我的编译目标一直都是Any CPU,还有什么别的东西可以检查吗?谢谢!

    2019年4月22日 3:35
  • Hi,

    试着调用Registry.LocalMachine:

      Dim myKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Lockheed Martin\Prepar3D v4", False)
            Dim value As String = CType(myKey.GetValue("SetupPath"), String)
    
            If Not String.IsNullOrEmpty(value) Then
                Me.Visible = False
            Else
                MessageBox.Show("You did not install any supported flight simulator.(Now supporting Lockheed Martin Prepar3D v4) Application will now exit.", "FS Not Found", MessageBoxButtons.OK)
                Application.Exit()
            End If
    https://stackoverflow.com/questions/9491958/registry-getvalue-always-return-null

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年4月22日 9:23
  • 您好,

    我尝试了使用这种方法,但是效果还是完全相同,打开应用之后马上会显示该messagebox然后程序退出,请问还有什么可能的解决办法吗?

    2019年5月11日 7:42
  • 你好,

    你的电脑系统是32位还是64位?切换你的编译的目标平台为x86或者x64,试一下返回是否还是nothing?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年5月14日 8:54
  • 你好,

    现在的情况是这样的,如果保留这些代码,我的Visual Studio就会在启动的时候检测该注册表项并返回Nothing,然后会导致VS每次打开的时候退出。虽然不太清楚为什么VS会执行这些代码,我还是打开了该用户控件的.vb文件然后删除了这些代码,但即使我将这些代码删除,只要我用VS打开vbproj文件,这行代码还是会执行,导致VS退出,所以我也没法去改编译目标平台。所以我删除了该用控件的三个文件,此时我可以正常启动VS,所以我尝试了更改编译目标,但无论改成x86还是x64,只要我把该用户控件重新添加进来,检查注册表时返回还是nothing。我的电脑时64位的。我的描述可能有点乱,但是大致意思应该能明白吧,我一会再尝试一下新建一个用户控件检测同样的注册表项是什么效果。

    2019年5月26日 3:09