列出注册表中某个位置的内容

时间:2017-07-06 12:39:14

标签: vb.net

我正在尝试使用以下内容填充表单上的列表框:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Fonts。我能够阅读"字体"中的特定条目的详细信息。并填充一个文本框,但我的愿望是只显示位于"字体"在列表框中。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Registry.LocalMachine及其OpenSubKey() method来打开注册表项以进行阅读。然后只需在其上调用GetSubKeyNames()即可检索其子键的所有名称:

Using FontKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts")
    For Each SubKey As String In FontKey.GetSubKeyNames()
        ListBox1.Items.Add(SubKey)
    Next
End Using

还将它放在代码文件的顶部:

Imports Microsoft.Win32

修改

由于上述方法似乎不适用于您,请尝试使用此方法手动关闭注册表项:

Dim FontKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts")
For Each SubKey As String In FontKey.GetSubKeyNames()
    ListBox1.Items.Add(SubKey)
Next
FontKey.Close()

编辑2:

从指定的值名称中获取值并不难,只需调用FontKey的{​​{3}}:

Dim FontKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts")
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()