Write values to the Registry (and use that to refresh the desktop icons) in C#

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 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 subkey 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;
}

The code gets the current desktop icon size from the Registry. It adds 1 and saves the new size in the Registry. 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.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.