Windows Forms: Hide and Show desktop icons in C#

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

To play the demo, you should create a simple UI that allows you to hide and show all desktop icons as shown below.

hide desktop icons in c#

You need to call Win32 API function to hide and show the desktop icons.

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

namespace HideDesktopIcon
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        [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);

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

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

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