Class
RegistryItem
Description
Creates and manages registry items on Windows.
Properties
Name |
Type |
Read-Only |
Shared |
---|---|---|---|
✓ |
|||
✓ |
|||
RegistryItem |
✓ |
||
✓ |
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
Name As String |
RegistryItem |
||
Name As String |
RegistryItem |
||
Source As RegistryItem |
|||
Name As String |
|||
Index As Integer |
RegistryItem |
||
Index As Integer |
|||
Index As Integer |
|||
name As String |
Property descriptions
RegistryItem.FolderCount
FolderCount As Integer
The number of child folders contained with the RegistryItem.
This property is read-only.
RegistryItem.KeyCount
KeyCount As Integer
The number of keys contained within the RegistryItem.
This property is read-only.
RegistryItem.Parent
Parent As RegistryItem
The RegisteryItem's parent. Note: You cannot get the parent of a hive. Each hive is considered a root.
This property is read-only.
RegistryItem.Path
Path As String
The full path to the RegistryItem.
This property is read-only.
Method descriptions
RegistryItem.AddFolder
AddFolder(Name As String) As RegistryItem
Adds the passed item as a folder. Returns a RegistryItem.
Similar to Child. If the folder with the name already exists, it'll be returned, otherwise it'll be created first
RegistryItem.Child
Child(Name As String) As RegistryItem
Returns the child named as a RegistryItem.
If the child does not exist, an exception (RegistryAccessErrorException) is raised.
RegistryItem.Constructor
Constructor(Source as RegistryItem)
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Clones the passed RegistryItem.
RegistryItem.Constructor
Constructor(Path as String, Create as Boolean = True)
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Creates or reads the registry item from the Path passed to it.
The optional parameter Create defaults to True; if you set it to False, the path will not be created if the path is not found. A RegistryAccessErrorException occurs when the path is not found (when create is False) or if you do not have permission to create the path (when create is True).
In order to create Registry keys, your app needs to be running with Administrator privileges.
On 64-bit Windows, registry keys for "HKEY_LOCAL_MACHINESOFTWARE" may instead be created in the "HKEY_LOCAL_MACHINESOFTWAREWOW6432Node" folder. Refer to the Microsoft document "32-bit and 64-bit Application Data in the Registry" for additional information about this.
Get the Windows product name from the Registry:
Var reg As New RegistryItem("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", False)
MessageBox(reg.Value("ProductName")) // Shows Windows product name
Create a new Registry key (Administer privileges required):
Var reg As New RegistryItem("HKEY_LOCAL_MACHINE\SOFTWARE\ACME")
reg = reg.AddFolder("MyApp")
reg.Value("Version") = 1.0
RegistryItem.DefaultValue
DefaultValue As Variant
Sets the default value of the RegistryItem.
RegistryItem.Delete
Delete(Name As String)
Deletes the passed RegistryItem.
RegistryItem.Item
Item(Index As Integer) As RegistryItem
Returns as a RegistryItem the RegistryItem corresponding to Index.
If Index is greater or equal than FolderCount, then an OutofBoundsException is raised. The index is zero based.
RegistryItem.KeyType
KeyType(Index As Integer) As Integer
Returns as an Integer the key type for the key located at the index.
The possible values are:
Key |
Value |
---|---|
Unsupported key |
|
REG_SZ |
0 |
REG_DWORD |
1 |
REG_BINARY |
2 |
REG_MULTI_SZ |
3 |
REG_EXPAND_SZ |
4 |
REG_QWORD |
5 |
RegistryItem.Name
Name(Index As Integer) As String
Returns as a String the name of the RegistryItem corresponding to Index.
If Index is greater or equal than KeyCount, then an OutofBoundsException is raised. Index is zero based.
RegistryItem.Value
Value(name As String) As Variant
Sets the RegistryItem value by the passed Index. If Index is greater or equal than KeyCount, then an OutofBoundsException is raised. Index is zero based.
When assigning Integer literal values, keep in mind that the Integer type size varies between 32-bit and 64-bit builds. In 32-bit builds, an Integer literal is 32-bit which results in a DWORD value set. In 64-bit builds an Integer literal is 64-bit which results in a QWORD value set. If your Registry value needs to be a DWORD in a 64-bit app, be sure to specifically set the Integer value type to 32-bit. You can do this using a specifically typed variable or the CType command:
// Use specific type
Var value As Int32 = 1100
reg.Value(App.ExecutableFile.Name) = value
// Set type using CType
reg.Value(App.ExecutableFile.Name) = CType(11000, Int32)
To create a binary key, use a MemoryBlock:
Var mb As New MemoryBlock(1)
mb.Byte(0) = 15
reg.Value("Key") = mb
Notes
The Value method returns valid information for expandable strings, binary and multi-strings. In the case of expandable strings, the string is automatically expands for you and returns it as a string. It supports getting and setting 64-bit values. The default value may also be 64-bit.
In the case of multi-strings, you are passed back a string that is Chr(0)-delimited. You can use the String.Split function to get the individual parts, like this:
part = regValue.ToArray(Encodings.UTF8.Chr(0))
where part is a String array.
In the case of a binary value, a MemoryBlock is returned. You cannot currently set an expandable string, or a multistring. To set a binary string, please pass in a MemoryBlock.
Sample code
This code displays the registry entries for the current Windows version:
// Get a registry key
Var reg As New RegistryItem("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", False)
Var lines() As String
// now we look on all values on this key
For i As Integer = 0 To reg.KeyCount - 1
Var name As String = reg.Name(i)
Var value As Variant = reg.Value(i)
lines.Add(name + " -> " + value)
Next
// and display them
Var s As String
s = String.FromArray(lines, EndOfLine)
MessageBox(s)
Compatibility
All project types on all supported operating systems.
See also
Object parent class; RegistryAccessErrorException error.