使用VBS操作注册表,通常使用RegRead/RegWrite/RegDelete方法,如:
RegRead:
'read.vbs(将以下代码存为read.vbs文件) Dim OperationRegistry Set OperationRegistry=WScript.CreateObject("WScript.Shell") Dim Read_Data1,Read_Data2 Read_Data1=OperationRegistry.RegRead("HKCR.xxf") '读取根键HKEY_CLASSES_ROOT之下的.xxf主键的默认值,并将该数据送至变量Read_Data1 Read_Data2=OperationRegistry.RegRead("HKCR.xxfvalue") '读取.xxf主键之下的value键值的数据,并将该数据送至变量Read_Data2 MsgBox("Default="&Read_Data1&" value="&Read_Data2) '将读取的数据显示出来
RegWirte:
'write.vbs Dim OperationRegistry Set OperationRegistry=WScript.CreateObject("WScript.Shell") Default=OperationRegistry.RegRead("HKCR") '获取一个空值 (null) OperationRegistry.RegWrite "HKCR.xxf",Default '在根键HKEY_CLASSES_ROOT之下新建主键.xxf,并置其默认值为空 OperationRegistry.RegWrite "HKCR.xxf","xxffile" '在根键HKEY_CLASSES_ROOT之下新建主键.xxf,并置其默认值?quot;xxffile" OperationRegistry.RegWrite "HKCR.xxfvalue1","string" '在主键.xxf之下新建一个字符串型键值value1,并置其初始值为"string" OperationRegistry.RegWrite "HKCR.xxfvalue2",1,"REG_DWORD" '在主键.xxf之下新建一个REG_DWORD型键值value2,并置其初始值为1 OperationRegistry.RegWrite "HKCR.xxfvalue3",0Xff,"REG_BINARY" '在主键.xxf之下新建一个二进制型键值value3,并置其初始值为十六进制的ff
RegDelete:
'delete.vbs Dim OperationRegistry Set OperationRegistry=WScript.CreateObject("WScript.Shell") OperationRegistry.RegRead("HKCR.xxfvalue") '删除.xxf主键之下的value键值 OperationRegistry.RegRead("HKCR.xxf") '删除根键HKEY_CLASSES_ROOT之下的.xxf主键
但是,如果要插入十六进制的数据就会有问题,如写入以下注册表键值,如果使用RegWrite,会报“类型不匹配”错误,因为RegWirte在写十六进制的值时,只支持一个值,不支持数组:
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settingsones3] "{AEBA21FA-782A-4A90-978D-B72164C80120}"=hex:1a,37,61,59,23,52,35,0c,7a,5f,20, 17,2f,1e,1a,19,0e,2b,01,73,13,37,13,12,14,1a,15,2a,4e,2c,08,0d,20,1b,28,18, 36,32
使用RegWrite方式
Dim OperationRegistry Set OperationRegistry=WScript.CreateObject("WScript.Shell") OperationRegistry.RegWrite "HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settingsones3","0xff,0x11","REG_BINARY"
报错如下:
--------------------------- Windows Script Host --------------------------- 脚本: C:WorkDocumentsBOCBOC.BancslinkVBSie_configie_forie - 副本.vbs 行: 3 字符: 1 错误: 类型不匹配 代码: 800A000D 源: Microsoft VBScript 运行时错误 --------------------------- 确定 ---------------------------
解决方法:
Const HKEY_CURRENT_USER = &H80000001 strComputer = "." Set objRegistry = GetObject _ ("winmgmts:\" & strComputer & " ootdefault:StdRegProv") strKeyPath = "SoftwareMicrosoftWindowsCurrentVersionInternet Settingsones3" strValueName = "{1EBA21FA-782A-4A90-978D-B72164C80120}" arrValues = Split("1a,37,61,59,23,52,35,0c,7a,5f,20,17,2f,1e,1a,19,0e,2b,01,73,13,37,13,12,14,1a,15,39,4e,2c,08,0d,20,1b,28,18,36,32", ",") For i = 0 To UBound(arrValues) : arrValues(i) = CByte("&H" & arrValues(i)) : Next errReturn = objRegistry.SetBinaryValue _ (HKEY_CURRENT_USER, strKeyPath, strValueName, arrValues)
参考:
http://www.cnblogs.com/heimirror/archive/2008/06/25/1229483.html
https://msdn.microsoft.com/de-de/library/aa393286%28v=vs.85%29.aspx
http://stackoverflow.com/questions/26967459/convert-hex-string-into-array-and-write-it-to-registry-vbscript