Working with Desktop Icons from Visual Basic

Monday Jun 22nd 2015 by Hannes Du Preez

Become proficient at dynamically running various Desktop icons and do some assorted Desktop functions from your VB program.


I have always been fascinated by Windows. That may sound odd, but allow me to explain. I have always been in awe of how Windows works, not the ordinary day-to-day workings; I mean how it works in the background. Today's topic is no exception! Today, I will explain how to dynamically run various Desktop icons and do some assorted Desktop functions from your VB program.

First, let's get complicated.

How Do the Icons Work in the Background?

You may see the ordinary icons daily, but have you ever wondered what makes them tick? I have, hence this article. A Desktop icon is a little program inside Windows that serves a certain function. For example, the Computer icon launches explorer.exe, which ultimately shows you the contents of your hard disk(s).

But, it is not actually a program itself; it can be described a system component. I will explain components in greater detail a bit later.

These icons are formally known as GUID objects.

What Are GUID Objects?

Here are links to two articles that will help you better understand GUID objects: what they are, and how to use them:

Our Project

Now that you have an understanding of what makes the Desktop icons tick, let us do a project that launches some of the icons and play around further with them.


Start Visual Studio and create a new VB.NET Windows Forms project. Give the project a nice descriptive name. Once the project has been created, design your form similar to Figure 1.

Figure 1: Our design

You may want to organize your buttons a bit better than mine...


Add the necessary Imports that we will need in our program:

Imports IWshRuntimeLibrary
'for APIs
Imports System.Runtime.InteropServices

I will explain these two namespaces shortly.

Now, add the code to launch the Computer icon from your program:

   Private Sub Button2_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button2.Click

      Process.Start("explorer.exe", _

   End Sub

With the preceding code, you use Process class' Start method to launch this program. The program that gets launched is explorer.exe and the arguments that you have supplied are:

/n: This means a new window

::{20D04FE0-3AEA-1069-A2D8-08002B30309D}: This the Computer object's GUID code

When run, explorer.exe will be launched, but it will display the Computer as accustomed to the normal Computer icon's display.

Here is more information regarding the Process class.

Add the following code to launch Windows Explorer normally:

   Private Sub Button6_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button6.Click

      Process.Start("explorer.exe", "/n,/e,C:\")
   End Sub

Here, explorer.exe also gets launched inside a new window, but we have SAID that it must display the contents of Drive C:\.

Add the following code to launch the My Documents icon:

   Private Sub Button4_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button4.Click

      Process.Start("explorer.exe", "/n, /e, _
   End Sub

Add the next code to launch the Network icon:

   Private Sub Button7_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button7.Click
      Process.Start("explorer.exe", _
   End Sub

Add the following code to launch the Recycle Bin:

   Private Sub Button1_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button1.Click
      Process.Start("explorer.exe", _

   End Sub

Now, it gets interesting...

Have you ever noticed that some Desktop icons have a shortcut menu assigned specifically to them? The Recycle Bin is no exception. If you were to right-click the Recycle Bin, you might get a menu that looks like Figure 2.

Figure 2: Recycle Bin shortcut menu

What I will show you next is how to dynamically empty the Recycle Bin. There is a bit more work associated with it, so here goes.... Add the following DLLImport declaration in your General Declarations:

   <DllImport("shell32.dll", _
         EntryPoint:="SHEmptyRecycleBin", _
         SetLastError:=True, CharSet:=CharSet.Auto)> _
      Private Shared Function SHEmptyRecycleBin _
         (ByVal hWnd As Integer, _
         ByVal pszRootPath As String, _
         ByVal dwFlags As Integer) As Integer
      End Function    'import API to empty recycle bin

The InteropServices Imports you added earlier come into play here; they make this type of declaration possible. This function is known as an API. If you do not know what APIs are, read the following:

Add the system constant to make the above API work, and not to show the progress of cleaning up your Recycle Bin:

   'don't show progress
   Private Const SHERB_NOPROGRESSUI As Short = &H2S

Now, add the following code to empty the Recycle Bin:

   Private Sub Button5_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button5.Click
      'return value for shemptyrecyclebin function
      Dim retvaL As Integer
      'show empty recyclebin dialog box
      retvaL = SHEmptyRecycleBin(Me.Handle.ToInt32, "", _

   End Sub

If you were to run your application now, you would see that the Recycle Bin gets cleared! Easy peasy.

Next, I would like to show you how to make a shortcut on your Desktop screen from within your program.

Add the following code:

   Private Sub Button9_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button9.Click
      Dim shell As WshShell = New WshShellClass
      Dim shortcut As WshShortcut = shell.CreateShortcut _
         ("C:\Users\Hannes\Desktop\notepad shortcut.lnk")
      shortcut.TargetPath = "C:\WINDOWS\notepad.exe"
      shortcut.IconLocation = "C:\WINDOWS\notepad.exe,0"
      shortcut.Description = "Launches Notepad"

   End Sub

Here, I have made use of the built-in capabilities of the WshShell object (the Imports we did earlier) to dynamically create a shortcut icon. The process is relatively simple and straightforward.

First, you create a WshShell object, then you use the CreateShortcut method inside the WshShortcut class to create the shortcut. All you need to supply is the Target of the application you'd like to create the shortcut for, then its Icon and Description, and voilà! A shortcut is created.

I have included a working sample with this article.


I hope you have enjoyed today's article on how to dynamically launch Desktop icons and use some of their functions. Until we meet again, cheers!

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved