How to Write a registry using Vbscript

Source: Internet
Author: User
Tags microsoft website

Category: Program
Users who used the DOS system earlier know that the MS-DOS operating system allows common users to write batch files for simple programming. It effectively simplifies our work and brings us a lot of convenience. At that time, there were even some programmers who specialized in writing batch files.

In fact, in addition to providing an easy-to-use interface, the operating system should also have a set of built-in Script languages. From a certain point of view, a batch processing file can be said to be a script language under the MS-DOS. In the Windows era, although the Windows system still retains the batch processing, the batch processing function in Windows is too simple to meet users' needs-for example, modifying the registry, can batch processing be performed? In fact, in Windows 98 and later Windows systems, apart from retaining the original batch processing system, they began to add support for the Scripting language, which is "Windows Scripting Host ", the Chinese name is "Windows Script Host" (WSH for short.

WSH itself is not a scripting language. It is the runtime environment of the scripting language. WSH supports JScript (Microsoft version of JavaScript, which is not the same as true JavaScript) and VBScript-this stuff must be familiar to everyone and is often used on webpages.

The script engine of WSH (Scripting engineers have two methods, one is the commandfang-style cscript.exe, and the other is Windows-style wscript.exe. The scripting program (plain text files with the extension ". js" and ". vbs") must be loaded and run using either of the two.

Although WSH supports JScript and VBScript languages, it must work with some objects provided by WSH, as well as the Properties and methods of the objects to maximize their effectiveness. We will not talk about JScript and VBScript programming here. We are still focusing on the WSH object. In fact, the Registry programming involved in this article almost does not require any knowledge about VBScript. We only need to simply apply the ready-made statements.

WSH objects include WScirpt, WshArguments, WshShell, WshUrlShortcut, WshNetwork, WshSpecialFolders, WshCollection, WshEnvironment, and WshShort.

Each object has its own attributes and methods. Here, we will only introduce the methods and attributes of the objects involved in accessing the registry.

The first is the WshShell object. This object can be used to set system environment variables and modify the registry data. To modify the registry data, you need to use the WshShell object in three methods: RegDelete (delete registry data), RegWrite (write or create registry data), and RegRead (read registry data ).

Generally, we only need to use the first two methods. Their usage is as follows:

1. RegWrite (write or create registry data)

* Create a subprimary key

Syntax: WshShell. RegWrite "subprimary key name"

For example, to create a subprimary key "HKEY_CURRENT_USERMyReg", use the following statement:

WshShell. RegWrite "HKCUMyReg"

Note: The subprimary key name must end with a backslash (). If you accidentally forget to enter this (), this method returns a key value, that means you want to create a key value named MyReg under HKEY_CURRENT_USER. Please pay attention to this 1.1!

Note that the subprimary key must start with one of the following root key names:

HKEY_CURRENT_USER (which can be abbreviated as HKCU), HKEY_LOCAL_MACHINE (HKLM), HKEY_CLASSES_ROOT (HKCR), HKEY_USERS, and HKEY_CURRENT_CONFIG
* Create a new key value under the subprimary key (or rewrite the data of an existing key value)

Syntax: WshShell. RegWrite "subprimary key name key value name", "key value data", "key value type"

For example, if you want to create a string key value KeyValue under the subprimary key "HKEY_CURRENT_USERMyReg" and set the key value to "str", you can use the following statement:

WshShell. RegWrite "HKCUMyRegKeyValue", "str"

(Note: When the key value is a string value, the "Key Value Type" statement can be omitted)

If the value is a binary value or a DWORD Value and the data is "1", the type of the key value must be declared as follows:

WshShell. RegWrite "HKCUMyRegKeyValue", 1, "REG_BINARY"

WshShell. RegWrite "HKCUMyRegKeyValue", 1, "REG_DWORD"

Note that data of binary and DWORD values cannot be enclosed by quotation marks, while data of string values must be enclosed by quotation marks.

Ii. RegDelete (delete registry data)

* Delete A subprimary key.

Syntax: WshShell. RegDelete "subprimary key name"

For example, to delete the subprimary key "HKEY_CURRENT_USERMyReg", use the following statement:

WshShell. RegDelete "HKCUMyReg"

* Delete A key value of the subprimary key.

Syntax: WshShell. RegDelete "subprimary key name key value name"

For example, to delete the key value KeyValue of the subprimary key "HKEY_CURRENT_USERMyReg", use the following statement:

WshShell. RegDelete "HKCUMyRegKeyValue"

Similar to RegWrite, a "" sign indicates the deletion of the subprimary key. If "" is not ", it indicates the key value under the subprimary key to be deleted.
In addition to the WshShell object, we must also understand the WScirpt object. The WScirpt object represents the Scripting Engine, which is automatically generated when the Engine is started. WScript objects provide methods for creating and reading objects. To use other WSH objects (such as WshShell objects), you must use the methods (CreateObject and GetObject) of the WScript object to create and read.

The syntax for creating an object is as follows:

WScript. CreateObject (strProgID)

StrProgID is the identifier of the object we want to create.

For example, to use the WshShell object and its attributes and methods, you must first use the CreateObject method of the WScript object to create a WshShell object. The statement is as follows:

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

For example. As we all know, if you use "run" in the "Start" menu, Windows will record the traces of "committing crimes" in the list box. In fact, the data is recorded in the registry HKEY_CURRENT_USERSoftwareMicrosoftWindows

Under the currentversionjavaserrunmru subkey, we only need to delete this subkey and re-establish it. Isn't that all right?

Now we can compile a script. Taking VBS as an example, we can use NotePad to create a new file, and then enter (the text after "//" is a comment without entering ):

// Define the object. to edit the registry, we need to use the WSHShell object and its method.

Dim WSHShell

// CreateObject method of the object to create the WSHShell object

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

// Then we use the RegDelete method of the WSHShell object to delete HKEY_CURRENT_USERSoftwareMicrosoft

Windowscurrentversionjavaserrunmru subkey

WSHShell. RegDelete "HKCUSoftwareMicrosoft

Windowscurrentversionpolicerrunmru"

// At last, we restore the primary key, recover the string value "MRUList" under the primary key, and set the data to an empty string.

WSHShell. RegWrite "HKCUSoftwareMicrosoft

Windowscurrentversionjavaserrunmrumrulist ",""

After the program ends, we can save the file as CleanMRU. vbs. Now we can use the Scripting Engine to execute this program. Suppose we saved this file in D: temp, then we can run the command --wcript.exe D: TEMPCleanMRU. vbs on the Startup menu. How about restarting? "Running" is already empty!

If we have compiled the program in this way, and finally we have to run it manually, it is better to directly use the Registry Editor each time! In fact, we can make it automatically loaded at every boot. Everyone knows this. We can use the Registry Editor in "HKEY_LOCAL_MACHINESoftwareMicrosoftWindows ".

CurrentVersionRun creates a string value "CleanMRU" under the subprimary key and sets its data to "Wcript.exe D: TEMPCleanMRU. vbs ". Hey, this is a success. After each boot, Windows will automatically execute the script to clear the running history.

This article only introduces the preliminary use of WSH objects related to registry modification. We only need to add some simple statements in the script to facilitate interactive operations-of course, this requires you to have more knowledge about WSH and VBScript. If you are interested, you can go to the Microsoft website to download WSH and VBScript help documentation, URL is http://www.microsoft.com/china/ scripting/windowshost/wshdoc.exe and http://www.microsoft.com/china/scripting/vbscript/download/ vbsdoc.exe (both Chinese ). In addition, there is a WSH folder under the Samples directory of Windows, which contains many. vbs and. js routines, you can open it and see it (right-click the icon and select "edit"). I believe there will be a lot of GAINS.

Finally, if you have installed Windows Scripting Host on your machine and the VBS program still cannot run, you may have modified the. VBS association by yourself or other applications. As far as I know, the association of the. VBS file will be modified when "Super Resolution" is installed on almost every machine. It doesn't matter. You just need to unmount it from "Control Panel"-"Add/delete programs"-"Windows Installer"-"attachment"-"details, then reinstall "Windows Scripting Host.

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.