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:
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.
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!