Working with Desktop Icons from Visual Basic

Introduction

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:

http://en.wikipedia.org/wiki/Globally_unique_identifier

https://msdn.microsoft.com/en-us/library/system.guid%28v=vs.110%29.aspx

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.

Design

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.

Icons1
Figure 1: Our design

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

Code

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", _
         "/n,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")

   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, _
         ::{450d8fba-ad25-11d0-98a8-0800361b1103}")
   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", _
         "/n,::{208D2C60-3AEA-1069-A2D7-08002B30309D}")
   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", _
         "/n,::{645FF040-5081-101B-9F08-00AA002F954E}")

   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.

Icons2
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:

https://msdn.microsoft.com/en-us/library/windows/desktop/ff818516%28v=vs.85%29.aspx

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, "", _
         SHERB_NOPROGRESSUI)

   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"
      shortcut.Save()

   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.

Conclusion

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!



Related Articles

Downloads

Comments

  • How to get the Code in C#

    Posted by abbaty48 on 06/25/2016 09:43am

    This article is nice, Please if you can help me with code in C#

    Reply
  • how to gather showrtcuts?

    Posted by alessandro demontis on 04/08/2016 07:43am

    Hi this article is very nice, i've been looking for something like this for a long time. But how to gather a list of all shortcuts already present in the desktop, instead of creating one? What if i want to create a text file where all shortcuts are listed, with their relevant targets and descriptions? Cheers

    Reply
  • How to collect info of icons currently on desktop

    Posted by Chris Raisin on 01/24/2016 04:23pm

    This article is great to add insight into how shortcuts work. I was aware of code on how to CREATE shortcuts, but does anyone know how to ascertain the properties of all shortcuts (and folders) on desktop of the current User? I have a need to get that info and store it for later retrieval (with a view of having a program you can use to restore your desktop after a Windows 10 "Reset")

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date