的完整路径写在HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
创新互联公司专注于企业成都营销网站建设、网站重做改版、皋兰网站定制设计、自适应品牌网站建设、H5网站设计、商城网站制作、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为皋兰等各大城市提供网站开发制作服务。
就可以自启动了。
以下是我前一段时间写的防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
以上。
饿的老狼
在代码顶部引入命名空间
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
利用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类从而
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