Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Registry Registry Operation by vbscript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces vbscript how to achieve Registry registry operation, the article is very detailed, has a certain reference value, interested friends must read it!

VBScript Code:

'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 that 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 an entire "tree" unless you' 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 the extension .vbs. The script is interpreted and executed by the wscript.exe file in the window interface and the cscript.exe file in the character interface. The command format is: cscript filename.vbs

Create object

To modify the registry with VBScript, you must first create an object that can communicate with the operating system, and then use various methods of the object to manipulate the registry. The method and format of creating this object are as follows:

Dim OperationReGIStry

Set OperationRegistry=WScript.CreateObject ("WScript.Shell")

The above code creates an object OperationRegistry that can communicate with the operating system.

Method of object

With the above object, it does not mean that we can operate on the registry right away, we must also find out several important ways for the object to operate on the registry.

1. RegRead for reading the registry

two。 RegWrite for writing to the registry

3. Delete the registry RegDelete

In addition, there are two general methods for WSH:

WScript.Echo () is used to display a string of text messages, which is equivalent to MsgBox () in VB.

Wscript.Quit () is used to exit the VBScript program.

Parameters of the method

For the above three operations, RegRead,RegWrite,RegDelete needs to be performed with parameters, and the number and form of parameters for these operations are different. Let me talk about one of their common and essential parameters:

This parameter can be called a "path parameter". It includes the root key, the primary key path, and the key value. Each part is represented as follows:

Root key:

There are two representations of the root key.

Method 1: directly use its string in the registry to represent it, such as:

HKEY_CLASSES_ROOT,HKEY_CURRENT_USER et al.

Method 2: express it with four abbreviations, the first two are HK, and the last two are the first letters of the root key word. Such as:

The root key HKEY_CLASSES_ROOT is expressed as: HKCR, and the root key HKEY_CURRENT_USER can be expressed as: HKCU, etc.

Primary key path:

The primary key path is the primary key location of the target key in the registry, and the primary keys are separated by the "\" symbol. For example, "Software\ Microsoft\ Windows\ CurrentVersion\ Policies\"

Key value:

The key value parameter is directly followed by the primary key path. For example, a complete path is as follows:

"HKCR\ Software\ Microsoft\ Windows\ CurrentVersion\ Policies\ NoRun"

Detailed explanation of the method

1. Detailed explanation of RegRead operation

The read operation RegRead is mainly used to read the data of the default value or key value of the primary key in the registry. We can send the read data to the corresponding variables, and then use the MsgBox () function in VB to display the data. This 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:

'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 send the data to the variable Read_Data1 Read_Data2=OperationRegistry.RegRead ("HKCR\ .xxf\ value")'to read the data of the value key under the .xxf primary key, and send the data to the variable Read_Data2 MsgBox ("Default=" & Read_Data1& "value=" & Read_Data2)'to display the read data.

2. Detailed explanation of RegWrite operation

The write operation RegWrite is mainly used to create a new primary key or key value in the registry and assign them an initial value. This operation can also modify the data with the existing primary key or key value in the registry, so the parameter structure of the write operation is more complicated than the read operation. It requires not only path parameters, but also an initial value and type parameters.

Let's start with the initial value parameter, which is essential for write operations and can be null but not omitted. When creating a new primary key, 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 parameter becomes the initial data of the new key value. The type of the initial value is determined by the type parameters. There are three main types:

(1) REG_SZ: character. This type is the default type

(2) REG_DWORD: double-byte type.

(3) REG_BINARY: binary type.

The first and second of the above three types are most used, and the third type can be replaced by the second in some cases. The assignment methods of these three types are as follows:

For REG_ SZ type: directly assigned with strings, such as "text", "string", etc.

For REG_ DWORD and REG_ BINARY, there are two assignment methods.

I) it is directly expressed in decimal numbers, such as 0, 1, etc.

Ii) is represented by hexadecimal numbers, such as: 0x12, 0xff, etc. Look at the example:

'write.VBs Dim OperationReGIStry Set OperationRegistry=WScript.CreateObject ("WScript.Shell") Default=OperationRegistry.RegRead ("HKCR\") gets a null value (null) OperationRegistry.RegWrite "HKCR\ .xxf\", Default' creates a new primary key .xxf under the root key HKEY_CLASSES_ROOT, and sets its default value to empty OperationRegistry.RegWrite "HKCR\ .xxf\", and "xxffile" creates a new primary key .xxf under the root key HKEY_CLASSES_ROOT and sets its default value? quot Xxffile "OperationRegistry.RegWrite" HKCR\ .xxf\ value1 "," string "create a new string key value value1 under the primary key .xxf, and set its initial value to" string "OperationRegistry.RegWrite" HKCR\ .xxf\ value2 ", 1," REG_DWORD "to create a new REG_ DWORD key value value2 under the primary key .xxf, and set its initial value to 1 OperationRegistry.RegWrite" HKCR\ .xxf\ value3 ", 0Xff "REG_BINARY" 'create a new binary key value value3 under the primary key .xxf and set its initial value to the hexadecimal ff

3. Detailed explanation of RegDelete operation

Delete operation RegDelete is mainly used to delete the primary key or key value that already exists in the registry, this operation is an extremely dangerous operation, it can "cut" the primary key or key value in the registry mercilessly, no matter how important the data below the key value, it can go smoothly, so you must be careful when using this operation.

The parameter form of the delete operation is almost exactly the same as that of the read operation, except that the delete operation does not need to send the return value of the operation to a variable, for example:

'delete.vbs Dim OperationRegistry Set OperationRegistry=WScript.CreateObject ("WScript.Shell") OperationRegistry.RegRead ("HKCR\ .xxf\ value")' delete the value key value under 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 primary keys or key values that already exist in the registry, let alone delete them, because improper write or delete operations to the registry will seriously cause the system to crash! If you really want to do this, please make a backup of the registry.

Application example

1. Read the "computer name" of this computer

'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 small arrow on the shortcut icon

'Hidden.vbs Dim HiddenArrowIcon Set HiddenArrowIcon=WScript.CreateObject ("WScript.Shell") Dim RegPath2,RegPath3 RegPath2= "HKCR\ lnkfile\ IsShortCut" RegPath3= "HKCR\ piffile\ IsShortCut" HiddenArrowIcon.RegDelete (RegPath2) HiddenArrowIcon.RegDelete (RegPath3)

3. Transform the "start" menu

'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) 'disable the run feature in the start menu Call Change (StartMenu_Find)' disable the find feature in the start menu Call Change (StartMenu_Close) 'disable the shutdown function in the start menu

4. Add a self-starting program to Windows

The program can run automatically when it is turned on.

'AddAutoRunProgram.vbs' assumes that 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" 'the full path file name of the self-startup program AutoRunProgram.Write RegPath&Key_Name,Key_Data,Type_Name' add the self-startup program autorun.exe MsgBox ("Success!") to the startup group. The above is all the contents of the article "how to operate the Registry registry in vbscript". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report