0

I have the following folder in registry

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Defender\Exclusions\Paths

But how can I show their value name to listbox?
Here is my code:

Dim FontKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows Defender\Exclusions\Paths")
For Each ValueName As String In FontKey.GetValueNames()
    Dim Value As Object = FontKey.GetValue(ValueName) 'Get the value (data) of the specified value name.
    If Value IsNot Nothing Then 'Make sure it exists.
        ListBox1.Items.Add(Value.ToString())
    End If
Next
FontKey.Close()

P/s: I get this error: (Because I do not have the requisite permissions to create a new key)

System.NullReferenceException: 'Object reference not set to an instance of an object.'

FontKey was Nothing.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • There's a difference between a `NullReferenceException` and the `SecurityException` that is actually thrown when you _**don't**_ have access to a registry key. In this case the former occurs because the key you opened doesn't exist, which is likely caused by your application viewing the 32-bit version of the registry key (`HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\...` rather than `HKEY_LOCAL_MACHINE\SOFTWARE\...`). Either compile your application as x64 or AnyCPU, or force it to view the 64-bit registry: https://stackoverflow.com/a/48603247/3740093 – Visual Vincent Aug 16 '18 at 07:31
  • how can i solve it? –  Aug 16 '18 at 07:38
  • Read the very last sentence of my comment. The link to my earlier answer explains a little more what's going on, along with a second solution. – Visual Vincent Aug 16 '18 at 07:40
  • I'm sorry, I have converted it to x64, but I only show 0 in my ListBox, how can I display value name of all keys? –  Aug 16 '18 at 07:41

1 Answers1

0

If you want to show the value name then just ignore retrieving the value and add the ValueName variable to the list box instead:

For Each ValueName As String In FontKey.GetValueNames()
    ListBox1.Items.Add(ValueName)
Next

As for the error:

There's a difference between a NullReferenceException and the SecurityException that is actually thrown when you don't have access to a registry key. In this case the former occurs because the key you opened doesn't exist, which is likely caused by your application viewing the 32-bit version of the registry key (HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\...) rather than the 64-bit version (HKEY_LOCAL_MACHINE\SOFTWARE\...).

To fix this, either compile your application as x64 or AnyCPU, or force it to view the 64-bit registry. See my answer here for more information: NullReferenceException when opening a subkey I know exists

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Oh, thank you! Visual Vincent, If you want to activate windows / office, you can contact me: https://www.facebook.com/dphuc23 –  Aug 16 '18 at 07:52
  • how can I write to folder: "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Defender\Exclusions\Paths"? –  Aug 16 '18 at 10:53
  • @DuyPhúc : [**`OpenSubKey(String, Boolean)`**](https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.opensubkey?redirectedfrom=MSDN&view=netframework-4.7.2#Microsoft_Win32_RegistryKey_OpenSubKey_System_String_System_Boolean_) – Visual Vincent Aug 16 '18 at 11:56
  • I use this (but it not work!) My.Computer.Registry.LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Defender\Exclusions\Paths", True).SetValue("Test", 0) –  Aug 16 '18 at 14:00
  • It show: System.NullReferenceException: 'Object reference not set to an instance of an object.' –  Aug 16 '18 at 14:01
  • @DuyPhúc : Remove `HKEY_LOCAL_MACHINE\ `, you've already opened it with `My.Computer.Registry.LocalMachine`. – Visual Vincent Aug 16 '18 at 14:56
  • sorry about this convenient, but I have this problem: I use: My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows Defender\Exclusions\Paths", True).SetValue("Test", 0) . But it show: System.Security.SecurityException: 'Requested registry access is not allowed.' –  Aug 16 '18 at 23:55
  • @DuyPhúc : Now you see what really happens when you don't have access to a registry key :). Try running your application with administrative privileges: https://stackoverflow.com/a/2818776 – Visual Vincent Aug 17 '18 at 07:40
  • If that doesn't work then I can't help you. You can try asking a new question, but it's not certain you'll get an answer. – Visual Vincent Aug 17 '18 at 07:43