VB.NET 参考代码:
十年专业网站制作公司历程,坚持以创新为先导的网站服务,服务超过成百上千家企业及个人,涉及网站设计、重庆APP开发公司、微信开发、平面设计、互联网整合营销等多个领域。在不同行业和领域给人们的工作和生活带来美好变化。
Imports Microsoft.Win32
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Windows", False)
For Each strSubKeyName In regKey.GetValueNames()
If regKey.GetValueKind(strSubKeyName) = RegistryValueKind.String Then
MessageBox.Show(regKey.GetValue(strSubKeyName))
' regKey.SetValue(strSubKeyName, "修改后的值")
End If
Next
End Sub
End Class
这里只是个读收藏夹位置例子,得到方法是调用自己函数GetFavs 大家可以看一下,原理自然就明白: 1、用RegOpenKey打开个键 2、用RegEnumValue重复枚举项目,直到找到合适为止 3、用RegCloseKey关闭打开键 ---------------------------------------------------------------------------------------------------------------操作注册表注意事项: 1、注册表API都以reg开头! 2、注册表API返回值若是0才表示成功! 3、操作好一个键一定要用RegCloseKey关闭! 4、其中用于存储数据值一定要声明,不可偷懒,否则~~嘿~辛苦已久代码会全报销! Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long Public Const HKEY_CURRENT_USER = H80000001Function GetFavs() Dim lngReg As Long, strName As String * 260, bteValue(259) As Byte, lngType As Long RegOpenKey HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", lngReg If lngReg Then Do While (RegEnumValue(lngReg, i, strName, 260, 0, lngType, bteValue(0), 260) = 0) If Left(strName, 9) = "Favorites" Then k = StrConv(bteValue, vbUnicode) k = Left(k, InStr(k, Chr(0)) - 1) GetFavs = k Exit Do End If i = i + 1 Loop RegCloseKey lngReg End If End Function
这个不属于标准C++的范畴,调用windows api即可
Win 95及NT的注册数据库(Registry)是系统中非常重要的组成部分。在Win32 API中有一组Reg函数来处理这些问题。其一般的读写过程如下:
1、使用RegOpenKeyEx或RegCreateKeyEx函数打开或创建一个键;
2、如果上一步成功,使用RegQueryValueEx读取子键的值,使用RegSetValueEx设置子键值,使用RegEnumKey获得所有子键,使用RegDeleteKey删除一个键;
3、完成操作后使用RegCloseKey关闭键。
下面这段程序打开HKEY_CURRENT_USER\Software\Zeal SoftStudio\AskPro FTP\LastTime键,然后读取WOL子键的值。
VB.NET主要是利用RegistryKey类封装的方法、属性等来进行与注册表相关的各种操作。
请参阅RegistryKey类
Imports Microsoft.Win32
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As String = Registry.CurrentUser.GetSubKeyNames("")
End Sub
End Class
需要 Microsoft.Win32命名空间。
Sub Button1Click(sender As Object, e As EventArgs)
'HKEY_CLASSES_ROOT\.doc
Dim Root1 As RegistryKey = Registry.ClassesRoot
'打开"SYSTEM"子健
Dim key1 As RegistryKey = Root1.OpenSubKey ( ".doc" ,true )
'打开".doc"子健
textBox1.Text =key1.GetValue(String.Empty)
End Sub