C# reads wrong registry data on 64-bit OS

后端 未结 3 1742
遇见更好的自我
遇见更好的自我 2021-01-04 11:54

I\'m working on a 64-bit Windows and my applicaiton runs with elevated privileges. I have a problem with the following very simple piece of code:

myKey = Reg         


        
相关标签:
3条回答
  • 2021-01-04 12:32

    This is a duplicate of another article. Here is some information that should help.

    How to read the 64 bit registry from a 32 bit application or vice versa

    0 讨论(0)
  • 2021-01-04 12:51

    This is by design, 32-bit programs have a different view of the registry than 64-bit programs. They are redirected to the HKLM\Software\Wow6432Node key when they try to read a value from the HKLM\Software hive. If you build your C# program with Project + Properties, Build tab, Platform Target = Any CPU then it will run as a 64-bit program and won't get redirected.

    32-bit programs can cancel the redirection but that's not easily done with the .NET RegistryKey class. P/Invoking RegOpenKeyEx with the KEY_WOW64_64KEY option is required. More info is available in this Windows SDK article.


    EDIT: this is now also available to .NET with the .NET 4 specific RegistryKey.OpenBaseKey() method. Pass RegistryView.Registry64 to view the registry the way a 64-bit process would.

    0 讨论(0)
  • 2021-01-04 12:56

    While i was running into same issue or same focus of the reason for this issue, I found a solution.

    Use this:

    //To set the Base to the right path set RegistryView to DEFAULT, Registry64 or Registry32 regarding the path you need. If you want to go over the x64 root:
    
    RegistryKey rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
            .OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
    

    Now it'll behave the way you want.

    Have fun!

    0 讨论(0)
提交回复
热议问题