Question Create a shortcut of a give file at a given place

Obelix

Member
Joined
Jul 3, 2012
Messages
5
Programming Experience
Beginner
Hello,

I'm student, i'm currently running my internship. They gave me a project witch is close to completing but i running into a problem;
i'm using visual basic 2010.

I want to make a shortcut from a file.
The filepath will be selected with a textbox.

My problem seems to be, making the shortcut.
I searched all over the internet but i can't find how to do it.

Thanks
 
Add a reference in your project to Windows Script Host Object Model (In the COM tab)
Add your imports statement

VB.NET:
Imports IWshRuntimeLibrary

Usage

VB.NET:
Dim F as new fileinfo("c:\path\file.txt")
Dim ShortcutLocation as string = "c:\users\name\desktop\"
Dim objShell As New IWshRuntimeLibrary.WshShell
Dim objShortcut As IWshRuntimeLibrary.WshShortcut

objShortcut = objShell.CreateShortcut(F.FullName & ".lnk")
objShortcut.TargetPath = ShortcutLocation & F.Name
objShortcut.Save()

Typed out, not done in IDE but should work fine.
 
Thanks for the quick response!
but i get an error in visual basic;

Dim F As New Fileinfo("c:\path\file.txt")

visual basic gives me an error (Type 'Fileinfo' is not defined.)
 
Imports IWshRuntimeLibrary
Imports System.IO


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim F As New FileInfo("C:\Nieuwe map (2)\test.txt")
Dim ShortcutLocation As String = "C:\Documents and Settings\test\"
Dim objShell As New IWshRuntimeLibrary.WshShell
Dim objShortcut As IWshRuntimeLibrary.WshShortcut

objShortcut = objShell.CreateShortcut(F.FullName & ".lnk")
objShortcut.TargetPath = ShortcutLocation & F.Name
objShortcut.Save()
End Sub
End Class
 
VB.NET:
Imports IWshRuntimeLibrary
Imports System.IO
 
 
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim F As New FileInfo("C:\Nieuwe map (2)\test.txt")
        Dim ShortcutLocation As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
        Dim objShell As New IWshRuntimeLibrary.WshShell
        Dim objShortcut As IWshRuntimeLibrary.WshShortcut
 
        objShortcut = objShell.CreateShortcut(F.FullName & ".lnk")
        objShortcut.TargetPath = ShortcutLocation & "\" & F.Name
        objShortcut.Save()
    End Sub
End Class
 
Back
Top