VB.NET中怎么读取注册表键值利用VB.NET访问注册表
译者注:访问注册表的例子比较多vb.net 查询注册表 , 然而通过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进行编程时,你可能会选择用VB.NET或.NET框架中的Registry类中的函数来访问注册表 。虽然大多数情况下VB.NET的内置函数足够用了,然而某些情况下你仍然需要.NET框架类来解决问题 。
注册表不仅存储了本地机上一些程序的信息,还保存了操作系统的信息 。操作注册表可能会有危险 。因此编程时必须谨慎的查看代码,确保程序对所运行的机器上的安全不会构成威胁 。
注册表入口点包括两部分:键名和键值 。入口点是存储在系统中的键和子键,类似于文件系统中的目录和子目录 。
必备知识
要想读懂本文需要有如下的必备知识:
1、熟悉上一个版本的Visual
Basic 。
2、注册表设计和利用的知识 。
3、理解访问注册表的安全含义 。
用VB.NET内置函数访问注册表
VB.NET提供了四个访问注册表的函数 , 为了使用它们,首先必须有读写权限 。任何运行在全信任模式下的代码都必须有访问注册表的必要的权限 。可以查看RegistryPermission类从而
vb.net 读取注册表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判断注册表是否存在在代码顶部引入命名空间
Imports Microsoft.Win32
Dim hkcu As RegistryKey = Registry.CurrentUser '打开主键到CurrentUser
Dim subKey As RegistryKey = hkcu.OpenSubKey("Software") '这里从CurrentUser打开了Softare子键vb.net 查询注册表,你可以直接将参数改成除了子键以外vb.net 查询注册表的注册表路径 。
If IsNothing(subKey) Then
'子键不存在
Else
'子键存在vb.net 查询注册表,判断有没有键值项
Dim obj = subKey.GetValue("aa") 'aa是要访问vb.net 查询注册表的键值项
If IsNothing(obj) Then
'键值项不存在
Else
'键值项存在
End If
End If
VB.NET 注册表读取VB.NET主要是利用RegistryKey类封装vb.net 查询注册表的方法、属性等来进行与注册表相关vb.net 查询注册表的各种操作 。
请参阅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命名空间 。
VB.net2010 操作注册表vb.net 查询注册表的完整路径写在HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
就可以自启动vb.net 查询注册表了 。
以下是vb.net 查询注册表我前一段时间写的防u盘自动运行的程序vb.net 查询注册表,里面可以找到如何操作注册表 。
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
【vb.net 查询注册表 vbs判断注册表项是否存在】Private Sub cmdDefault_Click()
Check0.Value = https://www.04ip.com/post/1
Check1.Value = https://www.04ip.com/post/0
Check2.Value = https://www.04ip.com/post/1
Check3.Value = https://www.04ip.com/post/0
Check4.Value = https://www.04ip.com/post/1
Check5.Value = https://www.04ip.com/post/0
Check6.Value = https://www.04ip.com/post/0
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdRecommend_Click()
Check0.Value = https://www.04ip.com/post/1
Check1.Value = https://www.04ip.com/post/1
Check2.Value = https://www.04ip.com/post/1
Check3.Value = https://www.04ip.com/post/1
Check4.Value = https://www.04ip.com/post/1
Check5.Value = https://www.04ip.com/post/0
Check6.Value = https://www.04ip.com/post/1
End Sub
Private Sub cmdSet_Click()
Dim hkey As Long
Dim lvalue As Long
Dim cddata As Long
Dim retval As Long
lvalue = https://www.04ip.com/post/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 retval0 Then
MsgBox "打开注册表失败,错误代码:"CStr(retval), vbExclamation, "错误"
End If
retval = RegQueryValueEx(hkey, "NoDriveTypeAutoRun", 0, REG_DWORD, lvalue, 4)
RegCloseKey hkey
If retval0 Then
MsgBox "读取注册表失败,错误代码:"CStr(retval), vbExclamation, "错误"
End If
ShowCheck (lvalue)
End Sub
Private Sub ShowCheck(lvalue As Long)
Check0.Value = https://www.04ip.com/post/lvalue Mod 2
lvalue = https://www.04ip.com/post/(lvalue - Check0.Value) / 2
Check1.Value = https://www.04ip.com/post/lvalue Mod 2
lvalue = https://www.04ip.com/post/(lvalue - Check1.Value) / 2
Check2.Value = https://www.04ip.com/post/lvalue Mod 2
lvalue = https://www.04ip.com/post/(lvalue - Check2.Value) / 2
Check3.Value = https://www.04ip.com/post/lvalue Mod 2
lvalue = https://www.04ip.com/post/(lvalue - Check3.Value) / 2
Check4.Value = https://www.04ip.com/post/lvalue Mod 2
lvalue = https://www.04ip.com/post/(lvalue - Check4.Value) / 2
Check5.Value = https://www.04ip.com/post/lvalue Mod 2
lvalue = https://www.04ip.com/post/(lvalue - Check5.Value) / 2
Check6.Value = https://www.04ip.com/post/lvalue Mod 2
End Sub
Private Function GetValue() As Long
GetValue = https://www.04ip.com/post/Check0.ValueCheck1.Value * 2Check2.Value * 4Check3.Value * 8Check4.Value * 16Check5.Value * 32Check6.Value * 64128
End Function
以上 。
饿的老狼
VB.net注册表操作问题微软在net里内置vb.net 查询注册表了一一些提供注册表操作vb.net 查询注册表的类vb.net 查询注册表 , 因此我们可以很轻松的操作注册表
导入名词称空间 imports Microsoft.Win32
Dim reg As RegistryKey
Dim regStr As String
reg = Registry.LocalMachine
reg = reg.OpenSubKey("SYSTEM\CurrentControlSet\Enum\USB", True)
regStr = reg.GetValue("VID_08E2PID_0006")
if regstr.tostring""then …复制文件
vb.net 查询注册表的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于vbs判断注册表项是否存在、vb.net 查询注册表的信息别忘了在本站进行查找喔 。
推荐阅读
- 歌曲怎么样下载到u盘,歌曲怎么样下载到U盘上
- ios9怎么降级到ios8,ios935降级到ios7
- 虚拟主播制作软件安卓版免费,虚拟主播模型制作软件
- mysql视图怎么找 mysql如何查询视图
- 包含异地平板游戏推荐ios的词条
- 美国真心话大冒险下载游戏,美国真人秀真心话大冒险第一季
- 快手直播怎样添加视频号,快手怎么添加直播内容到窗口
- 野兽下载,野兽 2019
- 如何拖动pdf,如何拖动pdf中的虚线框