Fastest Ways to Create Shortcuts in C# and VBNET - ByteScout
  • Home
  • /
  • Blog
  • /
  • Fastest Ways to Create Shortcuts in C# and VBNET

Fastest Ways to Create Shortcuts in C# and VBNET

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 shortcut quickly.

    1. Desktop Shortcut Creation
    2. Shortcut Creation for URLs
    3. Conclusion


Desktop Shortcut Creation

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;

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 using 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";

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"
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 which 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 which 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
End Sub

Shortcut Creation for URLs

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 = ""
End If
End Sub
End Class

This code snippet allows the creation of a shortcut which links the user to

Here’s a function which 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("URL=" + url);





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:

      1. They make the website extremely accessible; only one click away.
      2. Users don’t have to bother with remembering the name of the site.
      3. It saves time for the user.

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.


About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.