Add a Desktop Icon "Option" to your VB 2008 project

Use this board to post your code snippets - tips and tricks
7 posts Page 1 of 1
Contributors
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

first off goto
Project > Add Reference > COM Tab > scroll down to "Window Script Host Object Model" then click ok

Add this to the top of the form
Code: Select all
Imports IWshRuntimeLibrary
Imports Microsoft.Win32

Form_Load Event
Code: Select all
        If DesktopShortcutExist() = True Then
            ChkBoxDesktop.Checked = True
        ElseIf DesktopShortcutExist() = False Then
            ChkBoxDesktop.Checked = False
        End If

Then the function will look like this
Code: Select all
    Public Function DesktopShortcutExist() As Boolean
        Dim deskTopDir As String = My.Computer.FileSystem.SpecialDirectories.Desktop
        If IO.File.Exists(deskTopDir + "\" + Application.ProductName + ".lnk") Then
            Return True
        Else
            Return False
        End If
    End Function


In a Checkbox or Button event add this
Code: Select all
    Private Sub ChkBoxDesktop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkBoxDesktop.Click
        If ChkBoxDesktop.Checked = True Then
            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 & "\" + Application.ProductName + ".lnk"), IWshRuntimeLibrary.IWshShortcut)
            MyShortcut.TargetPath = Application.StartupPath + "\" + "Feather Editor v2.0t.exe"  'Specify target file full path
            MyShortcut.Save()
        Else
            Dim Desktop As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
            My.Computer.FileSystem.DeleteFile(Desktop + "\" + Application.ProductName + ".lnk")
        End If
    End Sub
What this does is set an shortcut on your desktop when you check the checkbox.
If you uncheck the checkbox it will delete the shortcut from your desktop.
Image
User avatar
upperdrag
Excellent Poster
Excellent Poster
Posts: 321
Joined: Fri Mar 12, 2010 12:05 pm

cool o.o, nice work :D
User avatar
Agust1337
Coding God
Coding God
Posts: 2456
Joined: Fri Feb 19, 2010 8:18 pm

Nice, Thanks for this :)
You're crazy!
I'm not crazy, my mother had me tested. ~Sheldon Cooper
Lewis
Coding God
Coding God
Posts: 1564
Joined: Sun Dec 20, 2009 2:12 pm

Nice work scottie :D
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

Awesome Scottie
cooll;
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
alex6848
VIP - Donator
VIP - Donator
Posts: 201
Joined: Sat Jan 16, 2010 10:26 pm

Nice :D
Free Facebook Page Likes - http://fbliker.tk/?ref=gillis
User avatar
RonaldHarvey
Top Poster
Top Poster
Posts: 113
Joined: Tue Apr 05, 2011 2:32 pm

This was one of my favorite apps creating a shortcut in desktop. Thanks for sharing Scottie, very nice. wahooo; clapper; cooll;
7 posts Page 1 of 1
Return to “Quick Snips”