2017-07-06 72 views
0

我想填充窗体上的列表框的内容:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Fonts。我能够读取“字体”中特定条目的详细信息并填充文本框,但我的愿望是只显示列表框中“字体”中的所有内容。任何人都可以协助在注册表中的位置列表内容

+0

可能的复制[如何递归列出在C#目录中的所有文件?](https://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c) – jitendragarg

+0

https:// stack overflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c 检查此。您可以将条目添加到列表中而不是打印。 – jitendragarg

回答

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:

充分利用指定的值名称值并不难,只需调用FontKeyGetValue() method:中

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() 
+0

上以“使用FontKey作为RegistryKey”开头的行中,该行用红色下划线表示...........''使用'类型'RegistryKey'的操作数必须实现'System.IDisposable' –

+0

@JeffreyRoccaforte: [**但它确实实现了'IDisposable' **(https://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey(v = vs.110).aspx),并且已经这样做了自.NET 1.1开始。 - 你的目标是什么版本的.NET Framework?你使用的是什么UI框架:Windows Forms(WinForms),WPF,UWP,ASP.NET?最后,你用什么VS版本? –

+0

@JeffreyRoccaforte:我编辑了我的答案。看看替代版本是否适合你。 –