十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
利用VB.NET访问注册表
10年积累的成都做网站、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有宣州免费网站建设让你可以放心的选择与我们合作。
译者注:访问注册表的例子比较多,然而通过VB.NET访问注册表的例子并不多,本文翻译了一篇MSDN上的利用VB.NET存取注册表的例子,挺详细也挺全面的。
(
)
Cat
Francis
Visual
Studio
Team
Microsoft
Corporation
April
2002
摘要:这篇文章描述了利用VB.NET内置函数DeleteSetting,
GetAllSettings,
GetSetting
和
SaveSetting,以及通用语言运行时的两个类Registry
和
RegistryKey来存取注册表的实例,详述了所需的权限,解释了何时利用注册表编辑器,并向你展示了如何利用程序从注册表中动态的读取数据及如何写入数据。
引言
当用VB.NET进行编程时,你可能会选择用VB.NET或.NET框架中的Registry类中的函数来访问注册表。虽然大多数情况下VB.NET的内置函数足够用了,然而某些情况下你仍然需要.NET框架类来解决问题。
注册表不仅存储了本地机上一些程序的信息,还保存了操作系统的信息。操作注册表可能会有危险。因此编程时必须谨慎的查看代码,确保程序对所运行的机器上的安全不会构成威胁。
注册表入口点包括两部分:键名和键值。入口点是存储在系统中的键和子键,类似于文件系统中的目录和子目录。
必备知识
要想读懂本文需要有如下的必备知识:
1、熟悉上一个版本的Visual
Basic。
2、注册表设计和利用的知识。
3、理解访问注册表的安全含义。
用VB.NET内置函数访问注册表
VB.NET提供了四个访问注册表的函数,为了使用它们,首先必须有读写权限。任何运行在全信任模式下的代码都必须有访问注册表的必要的权限。可以查看RegistryPermission类从而
的完整路径写在HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
就可以自启动了。
以下是我前一段时间写的防u盘自动运行的程序,里面可以找到如何操作注册表。
Option Explicit
Global Const REG_SZ As Long = 1
Global Const REG_DWORD As Long = 4
Global Const HKEY_CLASSES_ROOT = H80000000
Global Const HKEY_CURRENT_USER = H80000001
Global Const HKEY_LOCAL_MACHINE = H80000002
Global Const HKEY_USERS = H80000003
Global Const ERROR_NONE = 0
Global Const ERROR_BADDB = 1
Global Const ERROR_BADKEY = 2
Global Const ERROR_CANTOPEN = 3
Global Const ERROR_CANTREAD = 4
Global Const ERROR_CANTWRITE = 5
Global Const ERROR_OUTOFMEMORY = 6
Global Const ERROR_INVALID_PARAMETER = 7
Global Const ERROR_ACCESS_DENIED = 8
Global Const ERROR_INVALID_PARAMETERS = 87
Global Const ERROR_NO_MORE_ITEMS = 259
Global Const KEY_ALL_ACCESS = H3F
Global Const REG_OPTION_NON_VOLATILE = 0
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Private Sub cmdAbout_Click()
MsgBox "作者wolfccb不对本软件可能造成的任何错误或损失负责,请自行承担使用风险。", vbInformation, "About"
End Sub
Private Sub cmdDefault_Click()
Check0.Value = 1
Check1.Value = 0
Check2.Value = 1
Check3.Value = 0
Check4.Value = 1
Check5.Value = 0
Check6.Value = 0
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdRecommend_Click()
Check0.Value = 1
Check1.Value = 1
Check2.Value = 1
Check3.Value = 1
Check4.Value = 1
Check5.Value = 0
Check6.Value = 1
End Sub
Private Sub cmdSet_Click()
Dim hkey As Long
Dim lvalue As Long
Dim cddata As Long
Dim retval As Long
lvalue = GetValue
retval = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Policies\Explorer", 0, KEY_ALL_ACCESS, hkey)
retval = RegSetValueEx(hkey, "NoDriveTypeAutoRun", 0, REG_DWORD, lvalue, 4)
RegCloseKey hkey
If retval = 0 Then
MsgBox "设置已保存。", vbInformation, "提示"
Else
MsgBox "保存失败,错误代码:" + CStr(retval), vbExclamation, "错误"
End If
End Sub
Private Sub Form_Load()
Dim hkey As Long
Dim lvalue As Long
Dim cddata As Long
Dim retval As Long
retval = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Policies\Explorer", 0, KEY_ALL_ACCESS, hkey)
If retval 0 Then
MsgBox "打开注册表失败,错误代码:" + CStr(retval), vbExclamation, "错误"
End If
retval = RegQueryValueEx(hkey, "NoDriveTypeAutoRun", 0, REG_DWORD, lvalue, 4)
RegCloseKey hkey
If retval 0 Then
MsgBox "读取注册表失败,错误代码:" + CStr(retval), vbExclamation, "错误"
End If
ShowCheck (lvalue)
End Sub
Private Sub ShowCheck(lvalue As Long)
Check0.Value = lvalue Mod 2
lvalue = (lvalue - Check0.Value) / 2
Check1.Value = lvalue Mod 2
lvalue = (lvalue - Check1.Value) / 2
Check2.Value = lvalue Mod 2
lvalue = (lvalue - Check2.Value) / 2
Check3.Value = lvalue Mod 2
lvalue = (lvalue - Check3.Value) / 2
Check4.Value = lvalue Mod 2
lvalue = (lvalue - Check4.Value) / 2
Check5.Value = lvalue Mod 2
lvalue = (lvalue - Check5.Value) / 2
Check6.Value = lvalue Mod 2
End Sub
Private Function GetValue() As Long
GetValue = Check0.Value + Check1.Value * 2 + Check2.Value * 4 + Check3.Value * 8 + Check4.Value * 16 + Check5.Value * 32 + Check6.Value * 64 + 128
End Function
以上。
饿的老狼
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
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命名空间。