Settings for non-Web programs (desktop programs) are generally found in the registry. When performing automated testing of these programs, you need to deal with the registry frequently. Modify the program's settings by modifying the registry.
This chapter introduces how to use C# programs to operate the registry, especially how to operate the registry of a 64-bit operating system.
Reading Contents
Automatic testing often requires modification of the registry
Many system settings (such as IE settings) are found in the registry. The settings of desktop applications are also present in the registry. Therefore, when doing automated testing, you often need to modify the registry
Introduction to Windows Registration
Registry Editor is in C:/Windows/regedit.exe. Or, while running, run "regedit". You can start the registry editor.
The registry consists of primary keys, keys, subkeys, and value items. The following figure
The contents in the primary key: HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE are similar, one is the settings of the current user and the other is the settings of the machine.
C# Modify the registry
It is too simple to modify the registry in C#. First add using Microsoft.Win32; and then you will get it done. Look at the example below, read, add, delete, modify, and have all operations.
static void Main(string[] args) { // Instance, modify the IE homepage RegistryKey localMachine = Registry.CurrentUser; RegistryKey sougou = localMachine.OpenSubKey(@"SOFTWARE/Microsoft/Internet Explorer/MAIN", true); // Get IE homepage string version = sougou.GetValue("Start Page").ToString(); // Modify IE homepage sougou.SetValue("Start Page", "http://www.cnblogs.com/", RegistryValueKind.String); // Modify the Tanktest value item. If it does not exist, create a new TankTest value item. sougou.SetValue("TankTest2", "1", RegistryValueKind.DWord); // Delete the value item sougou.DeleteValue("TankTest2"); // Create a new subkey sougou.CreateSubKey("This is subkey1"); sougou.CreateSubKey("This is subkey2"); // Delete the subkey sougou.DeleteSubKey("This is subkey1"); }The difference between 32-bit operating system and 64-bit operating system registry
The above code is fine to run on a 32-bit operating system, but it won't work in a 64-bit operating system.
There are also 32-bit and 64-bit applications. In a 64-bit operating system, 32-bit applications and 64-bit applications can be run.
If you install a 32-bit application in a 64-bit operating system, it will be installed under C:/Program Files(x86)/. Start the task manager and you will see the process name of the 32-bit program and will bring a "*32" as shown below:
Note: In 64-bit operating system:
The registry for the 64-bit program is still in: HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Internet Explorer
The registry of the 32-bit program is instead in: HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/Microsoft/Internet Explorer
C# program accesses the registry of a 64-bit operating system
The C# programs are all 32-bit. When accessing the registry, they will access HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/, but they cannot access HKEY_LOCAL_MACHINE/SOFTWARE/
Previous .NET 3.5 C# programs needed to access the registry of the 64-bit operating system through Win32 API functions (with hundreds of lines of code required) to access the registry of the 64-bit operating system.
It is easy to access the registry of a 64-bit operating system after .NET 4.0.
static void Main(string[] args) { // Modify the registry of the 64-bit operating system// Modify the homepage of IE// Use RegistryView to specify whether it is a 64-bit operating system or a 32-bit RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry64); localKey = localKey.OpenSubKey(@"SOFTWARE/Microsoft/Internet Explorer/Main", true); if (localKey != null) { localKey.SetValue("Start Page", "http://www.cnblogs.com"); } }The above is the information about the automatic test reading and writing of the 64-bit operating system registry. We will continue to organize the relevant information in the future. Thank you for your support for this site!