在代码顶部引入命名空间
公司主营业务:网站建设、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出运城免费做网站回馈大家。
Imports Microsoft.Win32
Dim hkcu As RegistryKey = Registry.CurrentUser '打开主键到CurrentUser
Dim subKey As RegistryKey = hkcu.OpenSubKey("Software") '这里从CurrentUser打开了Softare子键,你可以直接将参数改成除了子键以外的注册表路径。
If IsNothing(subKey) Then
'子键不存在
Else
'子键存在,判断有没有键值项
Dim obj = subKey.GetValue("aa") 'aa是要访问的键值项
If IsNothing(obj) Then
'键值项不存在
Else
'键值项存在
End If
End If
Sub t()
'要查询的注册表路径
Key = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Hidden\SHOWALL"
FindValue = "CheckedValue" '要查询的注册表值
TempFile = Environ("TEMP") "\r.txt" '临时文件名
'利用命令提示符和reg命令执行注册表查询并输出到临时文件
Shell "cmd.exe /c reg.exe query " Key " " TempFile
While Dir(TempFile) = "" '查看临时文件是否生成
DoEvents
Wend
For i = 1 To 10000 '延时
DoEvents
Next
Open TempFile For Input As #1 '打开临时文件
S = Input(LOF(1), #1) '读取临时文件内容
Close #1 '关闭临时文件
If InStr(1, S, FindValue) 0 Then MsgBox "True" '判断是否存在要查询的注册表值
Kill TempFile ' '删除临时文件
End Sub
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
的完整路径写在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
以上。
饿的老狼
Dim a As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run")
If a.GetValue("查看数据.exe", "null").ToString = "null" Then
我自己写的一段代码,用getvalue函数,如果开机自动运行的注册表目录中不存在查看数据。exe这条数据,则返回null,不知道你能不能用上。
利用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或.NET框架中的Registry类中的函数来访问注册表。虽然大多数情况下VB.NET的内置函数足够用了,然而某些情况下你仍然需要.NET框架类来解决问题。
注册表不仅存储了本地机上一些程序的信息,还保存了操作系统的信息。操作注册表可能会有危险。因此编程时必须谨慎的查看代码,确保程序对所运行的机器上的安全不会构成威胁。
注册表入口点包括两部分:键名和键值。入口点是存储在系统中的键和子键,类似于文件系统中的目录和子目录。
必备知识
要想读懂本文需要有如下的必备知识:
1、熟悉上一个版本的Visual
Basic。
2、注册表设计和利用的知识。
3、理解访问注册表的安全含义。
用VB.NET内置函数访问注册表
VB.NET提供了四个访问注册表的函数,为了使用它们,首先必须有读写权限。任何运行在全信任模式下的代码都必须有访问注册表的必要的权限。可以查看RegistryPermission类从而