Work with Windows Registry via Delphi VCL App

How can we use it?

Publish date:

Overview

The Windows operating system and its applications store various data and settings in special files or databases.

One of the ways to store application and operating system settings is to do it through configuration files (files with the .ini extension).

The system registry is a specific way for the Windows operating system and its applications to store settings. It is a specialized database that stores settings for current users, applications, hardware information, and more. Settings and parameters in the Windows registry can be stored in the string format (REG_SZ), as 32-bit integers (REG_DWORD), in the binary form (REG_BINARY), etc. To access registry settings in Windows, there is a special utility called Regedit (registry editor).

After launching this utility, we will see the following root sections in the left part of the registry editor.

The HKEY_CLASSES_ROOT section is used to store and manage file associations.

The HKEY_CURRENT_USER section contains settings for the current user, whose credentials were applied to get access to the system. It stores most settings for the installed programs. It is also a link to the user’s profile in HKEY_USERS.

The HKEY_LOCAL_MACHINE section stores settings for Windows and applications for all users.

The HKEY_USERS section stores settings for all users of the system.

The HKEY_CURRENT_CONFIG section contains settings for all installed hardware. 

NoDrives registry parameter to hide disks in Windows Explorer

Registry parameter values can be modified manually using the registry editor or via working with the registry from applications with the help of specialized libraries. In this article, we will show you how you can edit registry parameter values in the Windows operating system using an Embarcadero Delphi VCL application.

As an example, we will demonstrate how to hide disk partitions from the user in the explorer. You will also learn how to change the icon and name of the desired disk partition using the settings in the Windows registry.

To hide drives in the explorer, we will change the Nodrives registry parameter of the DWORD type

(Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer).

We have a total of 26 disks (A – Z) at our disposal. This parameter may not be present in the registry by default. If its value is 0, then all disks are visible in the explorer. To hide each disk, a specific number must be written in the Nodrives parameter. For example, to hide disks A, B, C, and D, we need to set a binary one for each disk in the binary notation. This can be easily done with the help of the calculator in “programmer” mode.

Binary ones are set only for the four disks (A – D). In the decimal notation, the binary value 1111 equals 15.

Now, to hide these disks in the explorer, simply write the value 15 in the Nodrives parameter.

After restarting the system, we won’t see disks A-D in the explorer.

However, in a file manager like Total Commander, we can see disks C and D.

The letters A and B in the names of disks correspond to floppy drives. In modern computers, floppy disks are rarely used, and our PC doesn’t have drives A and B. However, we set binary ones for them as an example.

To hide all disks (there are 26 disks named from A to Z), you need to write the binary value with 26 ones (11111111111111111111111111) into the Nodrives parameter. In the decimal representation, this equals 67,108,863. If we want to hide only disk C, for example, then we need to write 100 in binary (which corresponds to 4 in decimal). Therefore, to hide only disk C, you need to write 4 in decimal format in the Nodrives parameter. The other disks can be hidden in a similar manner. First, it is necessary to set the values in the binary format and then convert them to decimal to enter the value in the Nodrives parameter.

How to set a custom icon and label for disk?

We will also consider the possibility of loading a custom icon (using the .ico format) and a label for each disk using the registry.

For this, we will use DefaultIcon and DefaultLabel (Computer\HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons).

In our example, we specified the path to the icon (a file with the .ico extension) and assigned a new label for disk C (MyDisk).

As a result of configuring the corresponding registry parameters, we set a custom icon for disk C and the label MyDisk.

Developing Embarcadero Delphi VCL app to work with registry

Let’s consider our Embarcadero Delphi VCL application and analyze the available possibilities of editing registry parameters. To load an icon for a disk, we will use the TOpenPictureDialog component (found in the Dialogs tab).

Image generation capabilities with Delphi FMX Application via OpenAI API
Check out our article
Check out

To hide or show disks in the Windows File Explorer, we need to use the TCheckBox component.

To select the letter of the desired disk, we will use the TComboBox component.

To input the label of the disk, we will use the TEdit component.

To apply drive visibility settings, open and apply drive icons, and set default icons for drives, we should use buttons of the TBitBtn class.

To work with the registry from our Embarcadero Delphi VCL application, we will connect the Registry library.

To hide or show drives in the File Explorer, we will create a constant DRIVECOUNT, which will be equal to 26 (the total number of disks A-Z). We will also declare an array DriveValues that will contain integers with binary ones to hide each disk from A to Z.

For example, to hide drive A, the binary value will be 1, which also equals 1 in decimal. For drive B, the binary value to hide it in the File Explorer will be 10, corresponding to 2 in decimal. For disk C, it will be 100 (decimal value 4). The same principle applies to the other drives that we have. Converting from binary to decimal is simple if you use a calculator in “Programmer” mode.

The configuration of showing or hiding disks in the File Explorer is implemented in the handler for the “Apply Drives Visibility” button. Let’s take a closer look at how it operates in our Embarcadero Delphi VCL application. To configure disk visibility through the registry, we will need to declare an object Reg (of class TRegistry), a variable DriveWord of the Integer type to store the visibility settings for each disk (A-Z). Also, we should declare a variable I that will be used in a loop (DriveWord will be set and subsequently stored in the system registry).

Next, we need to invoke the Application.MessageBox dialog, where the user can either apply the disk visibility settings or cancel the changes and exit the handler.

Afterward, we should call the constructor TRegistry.Create by default to initialize the Reg object. Then, using the OpenKey method, we will open the registry key to apply settings for the visibility of our drives. We assign the variable DriveWord a value of 0.

The value True will allow us to create this key if it does not already exist in the registry.

In the loop, we will actually determine the visibility of drives. We will iterate sequentially from A to Z. Using TCheckBox, we will set the visibility for each disk. If the checkbox is checked, the disk will be displayed in the File Explorer. Otherwise, it will be hidden. Objects of the TCheckBox class are named from CheckBox1 to CheckBox26 respectively, allowing us to configure the visibility of each disk individually. The FindComponent method will enable us to locate a component by its name and access it from the code.

Without it, we would have to write code to check the state of each TCheckBox (from CheckBox1 to CheckBox26). If a TCheckBox is unchecked, we should add the binary value (converted to decimal) from the DriveValues array to the variable DriveWord to hide the corresponding drive in the File Explorer (DriveWord := DriveWord + DriveValues[I]). This check will be performed for each of the 26 TCheckBox checkboxes.

Next, we should check the state of the CheckBoxAll toggle switch. If it is checked, we will write the value for hiding all drives A-Z in the Explorer to Nodrives (for this, we will declare the constant ALL_DRIVES = 67,108,863). If it is unchecked, we will write the value of DriveWord. To write integer parameters to the registry, we should use the WriteInteger method.

After that, we will need to close the registry key and release resources (Reg.CloseKey, Reg.Free). To apply our drive visibility settings in the Explorer, we will reboot the operating system (ExitWindows(EW_RebootSystem, 0)). For this, we should call Application.MessageBox, where we will offer the user to restart Windows.

The complete software code for the “Apply Drives Visibility” button handler is provided below.

1234567891011121314151617181920212223242526272829303132333435363738
procedure TForm1.BitBtn1Click(Sender: TObject);
var
Reg: TRegistry;
DriveWord: Integer;
I: Integer;
begin
if Application.MessageBox('Apply Drives Visible Settings?', 'Settings',
mb_IconWarning + mb_YesNo) = IDNO then
Exit
else
begin
Reg := nil;
try
DriveWord := 0;
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CURRENT_USER;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Policies\Explorer', True);
for I := DRIVECOUNT downto 1 do
begin
with Form1.FindComponent('CheckBox' + I.ToString) as TCheckBox do
begin
if not Checked then
DriveWord := DriveWord + DriveValues[I];
end;
end;
if CheckBoxAll.Checked then
Reg.WriteInteger('Nodrives', ALL_DRIVES)
else
Reg.WriteInteger('Nodrives', DriveWord);
Reg.CloseKey;
finally
Reg.Free;
end;
end;
if Application.MessageBox('Registry parameters were changed! Restart Windows?',
'Apply settings', mb_IconWarning + mb_YesNo) = IDYES then
ExitWindows(EW_RebootSystem, 0);
end;

Now let’s test the functionality of our Embarcadero Delphi VCL application, specifically its ability to manage the visibility of drives in Windows Explorer. Partitions from C to I available are available on our computer.

Let’s hide all drives, except I.

Now let’s click on “Apply Drives Visibility”.

Next, we’ll select “Yes”. After applying the settings, we will have an option to restart the system. We will click on “Yes” once again.

After the system restarts, we will check the visibility of drives in the File Explorer. As a result of applying our settings using the Embarcadero Delphi VCL application, only disk I is visible.

To hide all drives in the File Explorer, we will check the “Hide all drives” checkbox and then press “Apply Drives Visibility”.

After applying the settings, all drives in the File Explorer are hidden.

Now let’s consider the option of setting a custom icon for a specific drive in the File Explorer and labeling it. Loading the icon and setting its path for the desired drive is implemented in the handler for the “Open Icon and Apply Drives Icon” button. To store the path to our icon (.ico), we will declare a string field FIconPath.

We also need to declare an object Reg (TRegistry). Using OpenPictureDialog1, we will load the icon we need. The path to the icon will be stored in the field FIconPath (FIconPath := OpenPictureDialog1.FileName). We will also initialize the Reg object (TRegistry.Create).

Then, we will need to open the appropriate registry key to configure the icon for the desired drive (for example, Computer\HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\C\DefaultIcon for disk C). With the help of WriteString, we will write the path to the icon into the registry. Then, we will close the key (Reg.CloseKey).

Afterwards, we will proceed to setting the label for the desired drive. To do this, we need to open the key (for example, Computer\HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\C\DefaultLabel for disk C). We need to input the label using TEdit. Once the label is written, we will close the key and release resources.

Full code for the button handler “Open Icon and Apply Drives Icon” is provided below.

1234567891011121314151617181920212223242526
procedure TForm1.BitBtn2Click(Sender: TObject);
var
Reg : TRegistry;
begin
if not OpenPictureDialog1.Execute then
Exit
else
FIconPath := OpenPictureDialog1.FileName;
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\' +
ComboBox1.Text + '\DefaultIcon', True);
Reg.LazyWrite := True;
Reg.WriteString('', FIconPath);
Reg.CloseKey;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\' +
ComboBox1.Text + '\DefaultLabel', True);
Reg.LazyWrite := True;
Reg.WriteString('', Edit1.Text);
Reg.CloseKey;
ShowMessage('New Icon Successfully Applied!');
finally
Reg.Free;
end;
end;

Let’s test the functionality of our application to verify its ability to change disk icons and their labels.

As a result, we will see our custom icon for disk C and the label “MyDisk”.

Setting default icons for drives and labels is implemented in the handler for the “Set Default Icons for Drives” button. It will be enough just to delete the DriveIcons section from the registry using the DeleteKey method.

The complete software code for the handler is provided below.

1234567891011121314151617
procedure TForm1.BitBtn3Click(Sender: TObject);
var
Reg : TRegistry;
begin
if Application.MessageBox('Do you really want restore default icons for all drives?',
'Confirm', MB_ICONQUESTION + MB_YESNO ) = IDYES then
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.DeleteKey('Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons');
Reg.CloseKey;
finally
Reg.Free;
end;
end;
end;

Let’s test the functionality of our Embarcadero Delphi VCL application.

Now the icons for all disks and labels will be set to default.

Discover Softacom’s expert view on business software solutions
Get an insight into our services and follow new specialized publications on the blog
Get an insight

Subscribe to our newsletter and get amazing content right in your inbox.

This field is required
This field is required Invalid email address
By submitting data, I agree to the Privacy Policy

Thank you for subscribing!
See you soon... in your inbox!

confirm your subscription, make sure to check your promotions/spam folder

Subscribe to our newsletter and get amazing content right in your inbox.

You can unsubscribe from the newsletter at any time

This field is required
This field is required Invalid email address

You're almost there...

A confirmation was sent to your email

confirm your subscription, make sure to check
your promotions/spam folder