I am trying to populate a list box on a form with the contents of: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts. I am able to read the details of an specific entry within "Fonts" and populate a text box, but my desire is to just show everything that resides in "Fonts" in a list box. Can anyone assist?
Asked
Active
Viewed 2,217 times
1
-
Possible duplicate of [How to recursively list all the files in a directory in C#?](https://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c) – jitendragarg Jul 06 '17 at 12:48
-
https://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c Check this. You can add the entries to a list instead of printing. – jitendragarg Jul 06 '17 at 12:50
1 Answers
1
You can use Registry.LocalMachine
and its OpenSubKey()
method in order to open the registry key for reading. Then just call GetSubKeyNames()
on that to retrieve all the names of its sub keys:
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
Also put this in the top of your code file:
Imports Microsoft.Win32
EDIT:
Since the above doesn't seem to work for you try this method which closes the registry key manually:
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()
EDIT 2:
Getting the value from the specified value name isn't hard, just call the GetValue()
method of the FontKey
:
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()

Visual Vincent
- 18,045
- 5
- 28
- 75
-
on the line beginning with "Using FontKey As RegistryKey" the line is underlined in red saying........... 'Using' operand of type 'RegistryKey' must implement 'System.IDisposable' – Jeffrey Roccaforte Jul 06 '17 at 18:41
-
@JeffreyRoccaforte : [**But it does implement `IDisposable`**](https://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey(v=vs.110).aspx), and has done so since .NET 1.1. -- What version of .NET Framework are you targeting? And what UI framework are you using: Windows Forms (WinForms), WPF, UWP, ASP.NET? Finally, what VS version are you using? – Visual Vincent Jul 06 '17 at 19:53
-
@JeffreyRoccaforte : I edited my answer. See if the alternate version works for you. – Visual Vincent Jul 06 '17 at 20:05
-
THANK YOU.......I have learned something new! I really appreciate all of the help I am getting from the experts on this site like yourself. I just hope that someday, I will be good enough to answer questions and provide the same type of support I am receiving. – Jeffrey Roccaforte Jul 06 '17 at 20:29
-
@JeffreyRoccaforte : Glad I could help! When I joined Stack Overflow I too was not _**that**_ good at programming. However after frequent visits, attempting to answer questions and reading other questions and answers my knowledge grew exponentially. I believe you'll get to the answering part too some day if you stay in that mindset :). -- IMO this is the best programming website there is! – Visual Vincent Jul 06 '17 at 20:35
-
I have another question. I have changed ".GetSubKeyNames" to "GetValueNames" and this shows me the first column "Name" of all the fonts. I would like to be presented with that as well as the 3rd column "Data". I have been playing around with this for the last hour and can't seem to reach the goal? – Jeffrey Roccaforte Jul 07 '17 at 01:10
-
Probably need to change from listbox to datagrid? so that each column can be put somewhere specific ? – Jeffrey Roccaforte Jul 07 '17 at 02:03
-
@JeffreyRoccaforte : A `ListBox` should work fine if that's what you want to use, however just keep in mind that `GetValueNames()` will get the values _**inside the `Fonts` registry key**_ and _**not**_ its sub keys. That being said I don't have access to a computer and therefore doesn't know what that key looks like, but can you link me that code attempt of yours via http://pastebin.com/ and perhaps a screenshot of what happens when you run it? – Visual Vincent Jul 07 '17 at 07:01
-
******* This is the code behind the button Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim FontKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\fonts") For Each SubKey As String In FontKey.GetValueNames ListBox1.Items.Add(SubKey) Next FontKey.Close() End Sub – Jeffrey Roccaforte Jul 07 '17 at 11:04
-
The result is a list of the fonts (ValueName) residing in "CurrentVersion\Fonts". These "Values actually have 3 columns and I need to see all of the columns in the list box – Jeffrey Roccaforte Jul 07 '17 at 11:13
-
@JeffreyRoccaforte : Can you give me an example of what those values/value names are? That they have three columns doesn't tell me much... As I said I don't have access to a computer (I'm on vacation) so I can't check it myself. – Visual Vincent Jul 07 '17 at 13:17
-
Using ".GetValueNames" works wonderfully! The results received in the list box is indeed the font name. The field that is just as important to see is the font file name which is in a column with the tile of "Data" I am almost positive that what I need is ".GetValue". However, when I replace ".GetValueNames" with ".GetValue" it will not accept this change. – Jeffrey Roccaforte Jul 07 '17 at 17:45
-
@JeffreyRoccaforte : So by _column_ you mean _Value_... Give me a few mins. – Visual Vincent Jul 07 '17 at 17:48
-
@JeffreyRoccaforte : Hi again. When I wrote to you before I was busy writing another answer and then I completely forgot about this, I am really terribly sorry for that. I have updated my answer with the code for getting the value from the value name. – Visual Vincent Jul 07 '17 at 19:07
-