Results 1 to 4 of 4

Thread: Creating a Desktop Shortcut

  1. #1
    Christopherx is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    58
    Reputation
    56

    Creating a Desktop Shortcut

    How do I create A Desktop Shortcut ? Is there any particular command that will do it for me ?

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977
    Add 'Interop.IWshRuntimeLibrary.dll' to your project and import it's namespace too. Add this code:
    Code:
        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
    Then just call it passing a directory path and a shortcut title (the text that shows in windows):
    Code:
    CreateShortCut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "AwesomeApp.lnk"), "My Awesome app")
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  3. #3
    Flippedbeyond is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2008
    Posts
    198
    Reputation
    210

    Post Noticed Small Syntax Error in Your code

    Quote Originally Posted by JuggaloBrotha View Post
     
    Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName, IWshRuntimeLibrary.IWshShortcut)
    i noticed a small Syntax error, the line above is missing a closing ")" after the word FileName and should be the following instead:
    Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName), IWshRuntimeLibrary.IWshShortcut)
    Any feedback/critique/comments/suggestions on any my code is more than welcome! Thanks
    Happy Coding Everybody!
    -Flippedbeyond

  4. #4
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977
    Quote Originally Posted by Flippedbeyond View Post
    i noticed a small Syntax error, the line above is missing a closing ")" after the word FileName and should be the following instead:
    Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName), IWshRuntimeLibrary.IWshShortcut)
    Good catch, I've fixed my post.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking