Situatie
As Windows Registry is a very sensitive place to do anything, do not change any value predefined by the Windows System. This article will just be for learning purposes of how Python can be used to create some new registry keys with user-defined values. Windows registry can be used for different purposes, it can be accessed via typing regeditin the Run application.
Solutie
Pasi de urmat
Creating a new Key and Assigning a new value to it.
We will be creating a new key with a user-defined value under the HKEY_CURRENT_USER as that holds information about the current user only, not many confidential system files or information, HKEY_LOCAL_MACHINE holds the information related to the system and it is better not to tamper with them or create anything new which can affect the system.
winreg module is a built-in module that installs along with Python. so, we have to just import it.
- Firstly importing the module winreg with the alias wrg, users can use anything if they want or can use winreg also every time.
- Then defining the location where we would be creating the key and assign value.
- Then in “soft” variable passing the location and the name of the subfolder inside OpenKeyEx() method of winreg, which is used to open already made keys, it takes two arguments, first one the main folder name and then the subfolder mentioned as ‘key’ here.
- Then we are creating a new key whose name is Geeks inside the Software key.
- Then last two lines are providing some values and name, the REG_SZ is mentioned as we want to use a fixed-length string here, to keep it as simple as possible.
After creating and assigning values it is time to close the key otherwise it will remain open and other operations can’t be done.
Reading the contents of the created key.
Now after creating and assigning values it is time to fetch them, for that we will use the method QueryValueEx method and pass the exact location of the keys and a string that acts as a value to query. then we will close the key and print the two variables which were used to store the fetched values.
Leave A Comment?