VBScript Registry Registry Operation implementation code (read-write delete) _vbs

Source: Internet
Author: User
VBScript Code:
Copy Code code as follows:

' Create a WSH Shell object:
Set WshShell = CreateObject ("Wscript.Shell")
'
' Create a ' new key:
WshShell.RegWrite "hkcu\testkey\", "" "

' Create a new DWORD value:
WshShell.RegWrite "Hkcu\testkey\dwordtestvalue", 1, "REG_DWORD"

' Create a new subkey and a string value in ' New subkey:
WshShell.RegWrite "Hkcu\testkey\subkey\stringtestvalue", "Test", "REG_SZ"

' Read The values we just created:
WScript.Echo "Hkcu\testkey\dwordtestvalue =" _
& WshShell.RegRead ("Hkcu\testkey\dwordtestvalue")
WScript.Echo "Hkcu\testkey\subkey\stringtestvalue =" "" "_
& WshShell.RegRead ("Hkcu\testkey\subkey\stringtestvalue") & "" "

' Delete the subkey and key and the values they contain:
Wshshell.regdelete "Hkcu\testkey\subkey\"
Wshshell.regdelete "Hkcu\testkey\"

' Note:since the WSH Shell has no enumeration functionality, you cannot
' Use the WSH Shell object to delete a entire ' tree ' unless
' Know the exact name of every subkey.
' If you don ' t, use the WMI StdRegProv instead.

' Release the object '
Set WshShell = Nothing

The WSH program file, written in VBScript, has a. vbs extension, which is executed by the Wscript.exe file in the window interface, which is executed by the Cscript.exe file, and the command format is: cscript Filename.vbs
 
creating objects

To modify the registry in VBScript, you must first create an object that communicates with the operating system, and then manipulate the registry using various methods of the object, creating the method and format of the object as follows:
Dim Operationregistry
Set operationregistry=wscript.createobject ("Wscript.Shell")
The above code creates an object that communicates with the operating system Operationregistry
method of the object
With this object, it does not mean that the registry can be manipulated immediately, and we must also find out how the object operates on the registry in several important ways.
1. Read operations on the registry RegRead
2. Write operations on the registry RegWrite
3. The deletion of the registry operation RegDelete

To add, WSH also has two common methods:
WScript.Echo () is used to display a string of textual information, equivalent to the MsgBox () in VB.
Wscript.Quit () is used to exit the VBScript program.
 
parameter of the method
For all of the above three operations regread,regwrite,regdelete need to carry parameters, and the number and form of these operations are not the same, I will take a common and essential parameters of them:
This parameter can be called a path parameter, which includes the root key, the primary key path, and the key value, and the sections represent the following methods:
Root key:
There are two ways to represent a root key.
Method One: It is represented directly by a string in the registry, such as:
Hkey_classes_root,hkey_current_user, etc.
Method Two: With the abbreviation of four letters, the first two is HK, the last two is the first letter of the root key word. Such as:
The root key HKEY_CLASSES_ROOT is expressed as: HKCR, the root key HKEY_CURRENT_USER can be expressed as: HKCU, etc.
Primary Key Path:
The primary key path is the primary key location in the registry for the target key, separated by the "\" character between the primary keys. such as: "Software\microsoft\windows\currentversion\policies\"
Key value:
The key value parameter is directly connected to the primary key path. For example, a complete path is shown below:
"Hkcr\software\microsoft\windows\currentversion\policies\norun"
 
Method Detailed

   1, RegRead operation detailed

Read operation RegRead is mainly used to read the default value or key value data of the primary key in the registry. We can send the read data to the corresponding variable, and then use the MsgBox () function in VB to display the data, which achieves the purpose of reading the data in the registry ( You can also use the object Operationregistry method popup () to send the read data to the screen, for example:

Copy Code code as follows:

' Read.vbs (Save the following code as a read.vbs file)
Dim Operationregistry
Set operationregistry=wscript.createobject ("Wscript.Shell")
Dim Read_data1,read_data2
Read_data1=operationregistry.regread ("hkcr\.xxf\")
' reads the default value of the. XXF primary key under the root key HKEY_CLASSES_ROOT and sends the data to the variable read_data1
Read_data2=operationregistry.regread ("Hkcr\.xxf\value")
' Reads the data for the value key below the. XXF primary key and sends the data to the variable Read_data2
MsgBox ("default=" &Read_Data1& "value=" &read_data2)
' To display the read data


   2, RegWrite operation detailed

Write operation RegWrite is used primarily to create a new primary key or key value in the registry. And to give them an initial value, the operation can also modify data in the registry with the existing primary key or key value, so the parameter structure of the write operation is more complicated than the read operation, it needs not only the path parameter, An initial value and a type parameter are also needed.

Take a look at the initial value parameter, which is essential for write operations, it can be empty (null) but cannot be omitted. When a new primary key is created, the initial value parameter is assigned to the default value of the primary key, and when the new key value is created, the initial value is the initial data for the new key value. The type of the initial value is determined by the type parameter. There are mainly three kinds of types:
(1) REG_SZ: Character type. The type is the default type
(2) REG_DWORD: Double byte type.
(3) REG_BINARY: Binary type.

The above three types of 1th and 2nd are used most, and the 3rd type can be substituted with 2nd in some cases, these three types of assignment methods are as follows:
For REG_SZ type: directly with strings, such as "text", "string", etc.
There are two ways to assign a REG_DWORD type and a REG_BINARY type

i) directly in decimal notation, such as: 0,1 and so on.
ii) expressed in hexadecimal numbers, such as: 0X12,0XFF, etc. See Example:

Copy Code code as follows:

' Write. VBs
Dim Operationregistry
Set operationregistry=wscript.createobject ("Wscript.Shell")
Default=operationregistry.regread ("Hkcr\")
' Get a null value (NULL)
 
Operationregistry.regwrite "hkcr\.xxf\", Default
' Create a new primary key under the root key HKEY_CLASSES_ROOT. XXF, and set its default value to NULL
 
Operationregistry.regwrite "hkcr\.xxf\", "Xxffile"
' Create a new primary key under the root key HKEY_CLASSES_ROOT. XXF, and set its default value? quot;xxffile '
 
Operationregistry.regwrite "Hkcr\.xxf\value1", "string"
' Under primary key. XXF creates a new string-type key-value value1, with its initial value of "string"
 
Operationregistry.regwrite "Hkcr\.xxf\value2", 1, "REG_DWORD"
' Create a new REG_DWORD key value value2 under primary key. XXF, and place its initial value at 1
 
Operationregistry.regwrite "Hkcr\.xxf\value3", 0Xff, "REG_Binary"
' Under primary key. XXF Create a new binary key value Value3 and place FF with its initial value of 16



   3, regdelete operation detailed

Delete operation RegDelete is primarily used to delete a primary key or key value that already exists in the registry. This operation is an extremely dangerous operation, it can mercilessly in the registry "cut off", regardless of the key value of how important data, it can be unimpeded, so use this operation must be careful.

The Parameter form of the delete operation is almost exactly the same as the parameter form of the read operation, except that the deletion does not require the return value of the operation to be given to a variable, for example:

Copy Code code as follows:

' Delete.vbs
Dim Operationregistry
Set operationregistry=wscript.createobject ("Wscript.Shell")
Operationregistry.regread ("Hkcr\.xxf\value")
' Delete the value key below the. XXF PRIMARY Key
Operationregistry.regread ("hkcr\.xxf\")
' Delete the. XXF primary key under the root key HKEY_CLASSES_ROOT


To emphasize, do not change the existing primary key or key values in the registry, let alone delete them, because the registry has been improperly written or deleted operation, the situation will cause the system crashes! If you really want to do this, please make a backup copy of the registry.
 
Application Examples

1, reading machine "Computer name"
Copy Code code as follows:

' Readcomputername.vbs
Dim Readcomputername
Set readcomputername=wscript.createobject ("Wscript.Shell")
Dim Computername,regpath
Regpath= "Hklm\system\currentcontrolset\control\
Computername\computername\computername "
Computername=readcomputername.regread (Regpath)
MsgBox ("Computer name" &computername)



2, hide the shortcut icon on the small arrow
Copy Code code as follows:

' Hidden.vbs
Dim Hiddenarrowicon
Set hiddenarrowicon=wscript.createobject ("Wscript.Shell")
Dim Regpath1,regpath2
regpath1= "Hkcr\lnkfile\isshortcut"
Regpath2= "Hkcr\piffile\isshortcut"
Hiddenarrowicon.regdelete (REGPATH1)
Hiddenarrowicon.regdelete (REGPATH2)



3, the transformation of the "Start" menu
Copy Code code as follows:

' Changestartmenu.vbs
Dim Changestartmenu
Set changestartmenu=wscript.createobject ("Wscript.Shell")
Regpath= "Hkcr\software\microsoft\windows\currentversion\policies\"
Type_name= "REG_DWORD"
Key_data=1
 
Startmenu_run= "NoRun"
Startmenu_find= "Nofind"
Startmenu_close= "Noclose"
 
Sub Change (Argument)
Changestartmenu.regwrite Regpath&argument,key_data,type_name
MsgBox ("success!")
End Sub
 
Call Change (Startmenu_run) ' Disables the ' run ' feature in the Start menu
Call Change (Startmenu_find) ' Disables the ' Find ' feature in the Start menu
' Turn off system ' feature in the Start menu is disabled by call Change (Startmenu_close)



4. Add a self-starter program to Windows
The program can be run automatically when it is powered on.
Copy Code code as follows:

' Addautorunprogram.vbs
' Suppose the program is in the C:\myfile folder, the file name is Autorun.exe
Dim Autorunprogram
Set autorunprogram=wscript.createobject ("Wscript.Shell")
Regpath= "Hklm\software\microsoft\windows\currentversion\run\"
Type_name= "REG_SZ"
Key_name= "AutoRun"
Key_data= "C:\Myfile\autorun.exe"
' Full path filename for the self-starter program
Autorunprogram.write Regpath&key_name,key_data,type_name
' Add the Self-starter program to the Startup group Autorun.exe
MsgBox ("success!")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.