2017-07-06 67 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:我編輯了我的答案。看看替代版本是否適合你。 –