建议你使用VB.NET(VS2005+版本),操作注册表非常容易: For Each name As String In My.Computer.Registry.LocalMachine.OpenSubKey("software\microsoft\windows\currentversion\run").GetValueNames
十载的洪洞网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整洪洞建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“洪洞网站设计”,“洪洞网站推广”以来,每个客户项目都认真落实执行。
If (name "1425") Then
My.Computer.Registry.LocalMachine.OpenSubKey("software\microsoft\windows\currentversion\run").DeleteValue(name)
End If
Next
最简单的办法就是用shell
语句
添加和创建注册表项
shell
"cmd
/c
reg
add
hklm\software\myco
/v
data
/t
reg_binary
/d
fe340ead"
删除注册表项
shell
"cmd
/c
reg
delete
hklm\software\myco\myapp\timeout"
删除注册表项
timeout
及其所有子项和值
具体语法请在cmd中输入
reg/?
请使用VB.NET的“My”功能: '添加
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue("running", "C:\myapp.exe") ' 删除
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue("running")
这不是废话吗,DeleteSubKey只能删除调用实例的子项,并不能直接删除子子项。
赫赫,拿来主义
将下面代码保存为CLS文件 ,然后在工程中引用就可以了。
Attribute VB_Name = "BasRegisty"
Option Explicit
'读写注册表任何地方的函数
' 用法见下面
' 摘自杨光宏 (VB技巧手册)
'用于操作注册表的API函数定义 -begin-
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData 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, lpData As Any, ByVal cbData As Long) As Long
Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue 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 Any, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
'用于操作注册表的API函数定义 -end-
'用于操作注册表的常数定义 -begin-
'预定义的根结点
Public Const HKEY_CLASSES_ROOT = H80000000
Public Const HKEY_CURRENT_CONFIG = H80000005
Public Const HKEY_CURRENT_USER = H80000001
Public Const HKEY_DYN_DATA = H80000006
Public Const HKEY_LOCAL_MACHINE = H80000002
Public Const HKEY_PERFORMANCE_DATA = H80000004
Public Const HKEY_USERS = H80000003
'预定义的键值类型
Public Const REG_NONE = 0
Public Const REG_SZ = 1
Public Const REG_EXPAND_SZ = 2
Public Const REG_BINARY = 3
Public Const REG_DWORD = 4
Public Const REG_DWORD_LITTLE_ENDIAN = 4
Public Const REG_DWORD_BIG_ENDIAN = 5
Public Const REG_LINK = 6
Public Const REG_MULTI_SZ = 7
Public Const REG_RESOURCE_LIST = 8
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
#If WinNT Then
'打开注册文件的方式
Public Const KEY_EVENT = H1
Public Const KEY_NOTIFY = H10
Public Const KEY_QUERY_VALUE = H1
Public Const KEY_SET_VALUE = H2
Public Const KEY_CREATE_SUB_KEY = H4
Public Const KEY_ENUMERATE_SUB_KEYS = H8
Public Const KEY_CREATE_LINK = H20
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Const KEY_EXECUTE = (KEY_READ)
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
#End If
'注册文件错误描述
Const ERROR_SUCCESS = 0
Const ERROR_BADDB = 1009
Const ERROR_BADKEY = 1010
Const ERROR_CANTOPEN = 1011
Const ERROR_CANTREAD = 1012
Const ERROR_CANTWRITE = 1013
Const ERROR_REGISTRY_RECOVERED = 1014
Const ERROR_REGISTRY_CORRUPT = 1015
Const ERROR_REGISTRY_IO_FAILED = 1016
Const ERROR_NOT_REGISTRY_FILE = 1017
Const ERROR_KEY_DELETED = 1018
Const ERROR_NO_LOG_SPACE = 1019
Const ERROR_KEY_HAS_CHILDREN = 1020
Const ERROR_CHILD_MUST_BE_VOLATILE = 1021
Const ERROR_RXACT_INVALID_STATE = 1369
' 自定义注册文件错误
Const REGAGENT_NOKEY = -1002
Const REGAGENT_NOSUBKEY = -1003
Public glStatus As Long
'用于操作注册表的常数定义 -end-
Function gfsGetKeyStringValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As String
'功 能:从注册表中取得串值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: gfsGetKeyStringValue String 取得的注册表串值
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
Dim llBufferSize As Long '需读取串的串值长度
Dim lsKeyValue As String '存放读取的串值
'预先置为空
gfsGetKeyStringValue = Empty
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Function
End If
'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then '成功则取需读取字串的串值大小
glStatus = RegQueryValueEx(llKeyID, psSubKey, 0, REG_SZ, 0, llBufferSize)
If llBufferSize 2 Then '空值
glStatus = RegCloseKey(llKeyID)
Else '有值,正式读取串值
lsKeyValue = String(llBufferSize + 1, " ")
glStatus = RegQueryValueEx(llKeyID, psSubKey, 0, REG_SZ, ByVal lsKeyValue, llBufferSize)
If glStatus = ERROR_SUCCESS Then
gfsGetKeyStringValue = Left$(lsKeyValue, llBufferSize - 1)
End If
glStatus = RegCloseKey(llKeyID)
End If
End If
End Function
Function gflGetKeyBinaryValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As Long
'功 能:从注册表中取得二进制值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: gflGetKeyBinaryValue Long 取得的注册表二进制值
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
Dim llKeyValue As Long '存放读取的二进制值
'预先置为空
gflGetKeyBinaryValue = Empty
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Function
End If
'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then '成功则取二进制值
glStatus = RegQueryValueEx(llKeyID, psSubKey, 0, REG_BINARY, llKeyValue, Len(llKeyValue))
If glStatus = ERROR_SUCCESS Then
gflGetKeyBinaryValue = llKeyValue
End If
glStatus = RegCloseKey(llKeyID)
End If
End Function
Function gflGetKeyDwordValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As Long
'ok
'功 能:从注册表中取得双字值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: gflGetKeyDwordValue Long 取得的注册表双字值
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
Dim llKeyValue As Long '存放读取的双字值
'预先置为空
gflGetKeyDwordValue = Empty
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Function
End If
'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then '成功则取双字值
glStatus = RegQueryValueEx(llKeyID, psSubKey, 0, REG_DWORD, llKeyValue, Len(llKeyValue))
If glStatus = ERROR_SUCCESS Then
gflGetKeyDwordValue = llKeyValue
End If
glStatus = RegCloseKey(llKeyID)
End If
End Function
Sub gpvSetKeyStringValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal psKeyValue As String)
'ok
'功 能:设置注册表中的串值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' psKeyValue String 要设置的串值
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Sub
End If
'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then '成功则设置值
If Len(psKeyValue) = 0 Then '设为空值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0, REG_SZ, 0, 0)
Else '设为正常值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0, REG_SZ, ByVal psKeyValue, Len(psKeyValue) + 1)
End If
glStatus = RegCloseKey(llKeyID)
End If
End Sub
Sub gpvSetKeyDwordValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal plKeyValue As Long)
'ok
'功 能:设置注册表中的双字值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' plKeyValue Long 要设置双字值
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Sub
End If
'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then '成功则设置值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0, REG_DWORD, plKeyValue, Len(plKeyValue))
glStatus = RegCloseKey(llKeyID)
End If
End Sub
Sub gpvSetKeyBinaryValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal plKeyValue As Long)
'ok
'功 能:设置注册表中的二进制值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' plKeyValue Long 要设置的二进制值
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Sub
End If
'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then '成功则设置值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0, REG_BINARY, plKeyValue, Len(plKeyValue))
glStatus = RegCloseKey(llKeyID)
End If
End Sub
Function gflCreateKey(ByVal plKey As Long, ByVal psKey As String) As Long
'ok
'功 能:创建注册表中的主键
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' 输出: gflCreateKey Long 创建的主键ID
' 影响: glStatus Long 状态值
Dim llKeyID As Long '键ID
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置
glStatus = REGAGENT_NOKEY
Exit Function
End If
'创建主键
glStatus = RegCreateKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then
gflCreateKey = llKeyID
End If
End Function
Sub gpvDeleteKey(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String)
'ok
'功 能:删除注册表中的主键
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '键ID
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置
glStatus = REGAGENT_NOKEY
Exit Sub
End If
'利用创建主键判断是否存在主键
glStatus = RegCreateKey(plKey, psKey, llKeyID)
glStatus = RegDeleteKey(llKeyID, ByVal psSubKey)
End Sub
Sub gpvDeleteKeyValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String)
'ok
'功 能:删除注册表中的键值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '键ID
glStatus = ERROR_SUCCESS '假设成功
'确定参数有效
If Len(psKey) = 0 Then '主键未设置
glStatus = REGAGENT_NOKEY
Exit Sub
End If
'利用创建主键判断是否存在主键
glStatus = RegCreateKey(plKey, psKey, llKeyID)
glStatus = RegDeleteValue(llKeyID, ByVal psSubKey)
End Sub
'Function gfsGetKeyStringValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As String
'功 能:从注册表中取得串值
'Function gflGetKeyBinaryValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As Long
'功 能:从注册表中取得二进制值
'Function gflGetKeyDwordValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As Long
'功 能:从注册表中取得双字值
'Sub gpvSetKeyStringValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal psKeyValue As String)
'功 能:设置注册表中的串值
'Sub gpvSetKeyDwordValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal plKeyValue As Long)
'功 能:设置注册表中的双字值
'Sub gpvSetKeyBinaryValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal plKeyValue As Long)
'功 能:设置注册表中的二进制值
'Function gflCreateKey(ByVal plKey As Long, ByVal psKey As String) As Long
'功 能:创建注册表中的主键
'Sub gpvDeleteKey(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String)
'功 能:删除注册表中的主键
'Sub gpvDeleteKeyValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String)
'功 能:删除注册表中的键值