Write values in the Registry in C#

[Registry]

The example Read Registry values (to get the desktop icon size) in C# shows how to read values from the Registry. This example shows how to write values into the Registry and uses that ability to refresh the desktop icons when they get messed up.

The following SetRegistryValue method in the RegistryTools class writes a value into the Registry.

// Set a registry value.
public static void SetRegistryValue(RegistryKey hive,
    string subkey_name, string value_name, object value)
{
    RegistryKey subkey = hive.OpenSubKey(subkey_name, true);
    subkey.SetValue(value_name, value);
    subkey.Close();
}

The hive parameter indicates the part of the Registry to search. This should be something like Registry.ClassesRoot, Registry.CurrentConfig, Registry.CurrentUser, Registry.DynData, Registry.LocalMachine, or Registry.PerformanceData.

The method creates a RegistryKey object to work with a subkey inside the hive. The second parameter to OpenSubKey indicates that the method does need write access to the subkey.

The method then uses the subkey’s SetValue method to set the new value and then closes the subkey.

The following code shows how the example program refreshes the desktop icons.

private void btnRefreshIcons_Click(object sender, EventArgs e)
{
    Cursor = Cursors.WaitCursor;
    btnRefreshIcons.Enabled = false;
    Refresh();

    // Get the current icon size.
    object icon_size_string = RegistryTools.GetRegistryValue(
        Registry.CurrentUser,
        @"Control Panel\Desktop\WindowMetrics",
        "Shell Icon Size", 32);
    int icon_size = int.Parse(icon_size_string.ToString());

    // Add 1 and set the new size.
    icon_size++;
    RegistryTools.SetRegistryValue(
        Registry.CurrentUser,
        @"Control Panel\Desktop\WindowMetrics",
        "Shell Icon Size", icon_size);

    // Send HWND_BROADCAST to refresh the icons.
    IntPtr result;
    SendMessageTimeout(
        HWND_BROADCAST, WM_SETTINGCHANGE,
        SPI_SETNONCLIENTMETRICS, 0,
        SendMessageTimeoutFlags.SMTO_ABORTIFHUNG,
        10000, out result);

    // Restore the original value.
    icon_size--;
    RegistryTools.SetRegistryValue(
        Registry.CurrentUser,
        @"Control Panel\Desktop\WindowMetrics",
        "Shell Icon Size", icon_size);

    // Send HWND_BROADCAST to refresh the icons again.
    SendMessageTimeout(
        HWND_BROADCAST, WM_SETTINGCHANGE,
        SPI_SETNONCLIENTMETRICS, 0,
        SendMessageTimeoutFlags.SMTO_ABORTIFHUNG,
        10000, out result);

    Cursor = Cursors.Default;
    btnRefreshIcons.Enabled = true;
}

This code gets the current desktop icon size from the Registry. It adds one and saves the new size. It then uses the SendMessageTimeout API function to broadcast a message to all top-level windows indicating that there has been a change to the icons. After the top-level windows adjust to the new size, the program restores the original icon size in the Registry and again notifies the top-level windows.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in registry, system and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *