开发者

VB.NET read registry

开发者 https://www.devze.com 2023-03-25 23:48 出处:网络
Beyond perplexed this time... The simplest possible line of code works sometimes, sometimes it doesn\'t. First I thought the issue was that I was trying to read the value of a DWORD, but since I CAN

Beyond perplexed this time...

The simplest possible line of code works sometimes, sometimes it doesn't. First I thought the issue was that I was trying to read the value of a DWORD, but since I CAN read DWORD values from SOME keys, that must not be the problem. Now the problem seems to be that I can't read from ANY key if the key has a space in the name. Surely this can't be. I refuse to believe that MS didn't account for spaces in registry key paths and names.

So tell me why this doesn't work:

MsgBox(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\CA\CA ARCserve D2D\WebService", "Port", Nothing))

It just pops up an empty box. And yes, a value does exist in the registry, and yes, I have permission to read the key.

EDIT: Yup, over and over again it seems that you can't read from the registry if there are spaces anywhere in the key name. Seriously?!?

EDIT AGAIN: "Ramhound" says code examples are stupid. Fascinating point of view. However h开发者_如何学Pythonis own suggestion also failed:

Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\ATI Technologies\CBT")

Dim objValue As Object

objValue = key.GetValue("ReleaseVersion")

MsgBox(objValue.ToString())


After an entire wasted day, the solution is to set your VS project to "any cpu" in advanced compile options because if set to x86 and running on a 64bit OS you are limited to the "Wow6432node" in the registry.


It's also worth noting than on a 64 bit version of Windows 7 while running a vb.net app in 32 bit mode, the Wow6432Node key is hidden from you when using a Microsoft.Win32.RegistryKey object. I'd written this code to check which key I needed to read to get the right ODBC Driver subkey:

Dim myReg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
    Dim myReg_Key As Microsoft.Win32.RegistryKey

    myReg_Key = myReg.OpenSubKey("SOFTWARE")
    strRegistry_Keys = myReg_Key.GetSubKeyNames()
    bool64_Bit_OS = False

    For Each strSub_Key As String In strRegistry_Keys
        If strSub_Key = "Wow6432Node" Then
            bool64_Bit_OS = True
        End If
    Next

When you do a GetSubKeyNames() on the "SOFTWARE" key you are redirected to SOFTWARE\Wow6432Node

This does make it easier for my code as now I don't need to work out which subkey to look in to find which Oracle ODBC driver to use.

Kristian

0

精彩评论

暂无评论...
验证码 换一张
取 消