Senin, 27 Januari 2014

Create a shortcut using a computer code

Create a shortcut using a computer code

There are several ways to create a shortcut in Windows. All these methods converge to the use of Shell named Shell32. Relatively long time, even before the arrival of Windows XP, the file shell32.dll was used to provide many features that are now basic in the operating system Windows. This is the file that contains a number of icon used in Windows. What we do with the Shell32 is to use to create a Windows shortcut.
Create a shortcut


Again, since the method does not matter one way or another, it all comes with the following example.
The example used in this article can be downloaded at the end of this article.

If you want to make your program yourself, then try to reproduce a WinForm that looks like the image shown and observe the code written in Visual Basic:

Create a shortcut with Visual Basic

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Text = My.Application.Info.AssemblyName
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            If OpenFileDialog1.FileName.Length > 0 Then
                TextBox1.Text = OpenFileDialog1.FileName
            End If
        End If
    End Sub


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
       If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            If FolderBrowserDialog1.SelectedPath.Length > 0 Then
                TextBox2.Text = FolderBrowserDialog1.SelectedPath
            End If
        End If
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

        If IO.File.Exists(TextBox1.Text) Then
            If IO.Directory.Exists(TextBox2.Text) Then
                If TextBox3.Text.Length > 0 Then
                    CreateShortCut(TextBox1.Text, TextBox2.Text, TextBox3.Text)
                End If
            End If
        End If
    End Sub
    Private Function CreateShortCut(TargetName As String, ShortCutPath As String, ShortCutName As String) As Boolean
        Dim oShell As Object
        Dim oLink As Object
        you don’t need to import anything in the project reference to create the Shell Object

        Try

            oShell = CreateObject("WScript.Shell")
            oLink = oShell.CreateShortcut(ShortCutPath & "" & ShortCutName & ".lnk")

            oLink.TargetPath = TargetName
            oLink.WindowStyle = 1
            oLink.Save()
        Catch ex As Exception

        End Try

    End Function
End Class




All lines of code inside functions Button1, Button2 and Button3 are purely cosmetic. The heart of the application lies in the few lines inside the function that takes as parameters CreateShortcut 3 information: The target file will be saved where the shortcut and the shortcut name.
There is very little to say or add to the exception that there are other options in the object invisible because it is a COM type library (UNMANAGED) and need to constantly refer to articles like this to remember to use it.

Download the sample project here: CreateShortcutProgrammatically.zip


Related Posts by Categories

0 komentar:

Posting Komentar