Windows Forms: Hide and Show desktop icons in C#

This post shows you How to hide and show desktop icons in C# Windows Forms Application.

How to hide desktop icons programmatically?

You can use the Win32 API function to hide and show desktop icons in c#.

First of all, you should create a simple GUI that allows you to hide and display all the icons on the screen as shown below.

hide desktop icons in c#

Once you've finished designing your form, you need to open your code behind, then add ShowWindow and FindWindowEx methods as shown below.

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

The DLLImport ("user32.dll") must have the name of C++ and Parentheses indicate that we are using an attribute.

Adding a click event handler to the Show button allows you to show desktop icons.

private void btnShow_Click(object sender, EventArgs e)
{
    IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
    ShowWindow(hWnd, 5);
}

Finally, Add the click event hanlder to the Hide button allows you to hide desktop icons.

private void btnHide_Click(object sender, EventArgs e)
{
    IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
    ShowWindow(hWnd, 0);
}

And don't forget to include the namespaces below to your form.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

Another way you can use registry to remove all the icons in the desktop in C# by manipulating the Windows registry.

Go HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, then add

Name: HideIcons 
Type: REG_DWORD
Value: 1

Here's a simple example of how you can achieve this in c# code

// Registry key for desktop icons visibility settings
const string desktopIconsKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced";
// Get the current value of the desktop icons visibility setting
int currentValue = (int)Registry.GetValue(desktopIconsKey, "HideIcons", 0);
// Toggle the value to hide/show desktop icons
int newValue = (currentValue == 0) ? 1 : 0;
// Set the new value
Registry.SetValue(desktopIconsKey, "HideIcons", newValue, RegistryValueKind.DWord);

This program toggles the visibility of desktop icons by changing the value of the registry key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideIcons between 0 and 1.

Through this c# example, i hope so you can easily hide or show desktop icons programmatically in c# windows forms application.