A shortcut or a “Shell Link” is basically a link to another file or object in your directory, which can also reference URLs (websites). This article will show you how to use C# and VBNET to create desktop as well as Internet shortcuts, to allow easier, faster access to resources, whatever your usage needs are.
Shortcuts are a commonly-used tool, as they are used by installers to allow the user to easily install programs simply by clicking on an icon on their desktop. Commonly they are used to reference a “local object” on the system – when they are, they’re referred to as file-based shortcuts. Non-file-based shortcuts (for printers and scanners, as well as other functions) can also be implemented, but this presents significant challenges, as one would have to use the shell namespace and try to create a shortcut with a low-level programming language. For now, let’s focus on how to make shortcuts quickly.
A file-based object is basically anything inside a computer drive or directory, so a shortcut providing a link to a file of any sort is a “File-based Shortcut”.
Here’s a code snippet in C# that will create one such shortcut.
var startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); var shell = new WshShell(); var shortCutLinkFilePath = Path.Combine(startupFolderPath, @"\CreateShortcutSample.lnk"); var windowsApplicationShortcut = (IWshShortcut)shell.CreateShortcut(shortCutLinkFilePath); windowsApplicationShortcut.Description = "How to create short for application example"; windowsApplicationShortcut.WorkingDirectory = Application.StartupPath; windowsApplicationShortcut.TargetPath = Application.ExecutablePath; windowsApplicationShortcut.Save();
This code creates a shortcut on the user’s desktop. The first section gives the path to the user’s desktop, using the Windows Script Host Object Model library. The use of this library means that the library must be included in the project for this code to work.
You can also create shortcuts to special icons like the Recycle Bin, which the following does use C#.
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); object shDesktop = (object)"Desktop"; WshShell shell = new WshShell(); string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Recycle Bin.lnk"; IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); shortcut.Description = "New shortcut for Recycle Bin"; shortcut.Hotkey = "Ctrl+Shift+N"; shortcut.IconLocation = @"C:\WINDOWS\System32\imageres.dll"; shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\Recycle.Bin"; shortcut.Save();
Visual Basic also allows this functionality. Here’s an example of a desktop shortcut creation using Visual Basic.
option explicit
‘ Routine to create “mylink.lnk” on the Windows desktop.
sub CreateShortCut() dim objShell, strDesktopPath, objLink set objShell = CreateObject("WScript.Shell") strDesktopPath = objShell.SpecialFolders("Desktop") set objLink = objShell.CreateShortcut(strDesktopPath & "\mylink.lnk") objLink.Arguments = "c:\windows\tips.txt" objLink.Description = "Shortcut to Notepad.exe" objLink.TargetPath = "c:\windows\notepad.exe" objLink.WindowStyle = 1 objLink.WorkingDirectory = "c:\windows" objLink.Save end sub
‘ Program starts running here.
call CreateShortCut()
In this code, the program starts executing when the “CreateShortCut” function is called. This routine creates the COM object which leads to the declaration of a variable that has the desktop path contained in it which allows the construction of the desktop shortcut.
Finally, here’s a Visual Basic code example for a desktop shortcut that can invoke an executable file – that means that this code provides the user with the functionality to install programs by clicking on their desktop icons.
Imports IWshRuntimeLibrary Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click Dim WshShell As WshShellClass = New WshShellClass Dim MyShortcut As IWshRuntimeLibrary.IWshShortcut ' The shortcut will be created on the desktop Dim DesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) MyShortcut = CType(WshShell.CreateShortcut(DesktopFolder &"\MyShortcutName.lnk"), IWshRuntimeLibrary.IWshShortcut) MyShortcut.TargetPath = Application.StartupPath & "\YourApp.exe" 'Specify target app full path MyShortcut.Save() End Sub
Now we take a brief look at creating shortcuts for URLs. The need for URL shortcuts arises from the fact that users may access resources or content online. With these shortcuts, you can just click the icon to visit any website of your choice. The Internet shortcut file we’ll create has an extension of .URL, which is recognized by all Operating Systems and all major browsers.
Here’s a code example for creating a shortcut to a website in Visual Basic.
Imports IWshRuntimeLibrary Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fbd As New FolderBrowserDialog If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then Dim WshShell As New WshShellClass Dim MyShortcut As IWshRuntimeLibrary.IWshShortcut MyShortcut = CType(WshShell.CreateShortcut(fbd.SelectedPath & "\your post.lnk"), IWshRuntimeLibrary.IWshShortcut) MyShortcut.TargetPath = " https://www.facebook.com/" MyShortcut.Save() Process.Start(fbd.SelectedPath) End If End Sub End Class
This code snippet allows the creation of a shortcut that links the user to Facebook.com.
Here’s a function that allows the creation of an internet shortcut in a similar way using C#.
private void CreateShortcut(string name, string url) { string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); using (StreamWriter writer = new StreamWriter(deskDir + "\\" + name + ".url")) { writer.WriteLine("[InternetShortcut]"); writer.WriteLine("URL=" + url); writer.Flush(); } }
If you are looking to add shortcuts in VBNET without installers, you use the ShellLink.cs available at vbAccelerator. However, you need to add Windows Script Host Object Model reference.
To do so, you need to go to your Project. Once there, you now need to choose “Add Reference.” Now choose “COM” and then finally, “Windows Script Host Object Model.”
The code for creating the shortcut without installers in VBNET is as follows.
Imports IWshRuntimeLibrary Private Sub CreateShortCut(ByVal FileName As String, ByVal Title As String) Try Dim WshShell As New WshShell ' short cut files have a .lnk extension Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName, IWshRuntimeLibrary.IWshShortcut) ' set the shortcut properties With shortCut .TargetPath = Application.ExecutablePath .WindowStyle = 1I .Description = Title .WorkingDirectory = Application.StartupPath ' the next line gets the first Icon from the executing program .IconLocation = Application.ExecutablePath & ", 0" .Arguments = String.Empty .Save() ' save the shortcut file End With Catch ex As System.Exception MessageBox.Show("Could not create the shortcut" & Environment.NewLine & ex.Message, g_strAppTitleVersion, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
The above code is easy to understand. First, you need to create a Function where you will define the code. Here, you create a shortcut using the IWshRutntimeLibrary. If the shortcut is created successfully, then you set the.TargetPath as the Application.ExecutablePath and also set other variables that can help you define based on your requirement.
If you are interested in using a library for creating the shortcut, then you can also use the InternetShortcut class. The class will enable you to create an internet shortcut URL. We discussed earlier how you can create internet-based shortcuts, however, we didn’t mention the use of InternetShorcut Class.
If you are creating an Application shortcut, then you need to use the following code.
private void appShortcutToDesktop(string linkName) { string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url")) { string app = System.Reflection.Assembly.GetExecutingAssembly().Location; writer.WriteLine("[InternetShortcut]"); writer.WriteLine("URL=file:///" + app); writer.WriteLine("IconIndex=0"); string icon = app.Replace('\\', '/'); writer.WriteLine("IconFile=" + icon); } }
The code snippets and methods that we have listed here are crafted for the 32-bit systems. However, if you are looking to create shortcuts for a 64-bit system, then you find it challenging. You need to take advantage of the Shell and Shlwapi DLL versions and also ensure that you change your target platform and platform target variables to x64.
Nowadays, many blogs and websites are creating these files dynamically on their servers and allowing their users to download these internet shortcuts to their websites, because:
Shortcuts can either be file-based or URL-based, and both Visual Basic and C# offer a wide variety of techniques for shortcut creation. Thus, programmatic shortcut creation should be incorporated as a permanent programming habit, as they allow the user to save time and effort.