Click here to Skip to main content
15,891,657 members
Articles / Programming Languages / Shell
Tip/Trick

Pin items to the Start Menu

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
10 Feb 2010CPOL 17.3K   4   2
Ever want to pin a file to the Start Menu? This is a simple class I built that allows you to do just that. It works on XP, Vista, and Windows 7. I've included a small example at the bottom of this post.#Region " Pinner Class " Public Class smPinner Private _isPinned As...
Ever want to pin a file to the Start Menu? This is a simple class I built that allows you to do just that. It works on XP, Vista, and Windows 7. I've included a small example at the bottom of this post.

#Region "  Pinner Class  "
    Public Class smPinner
        Private _isPinned As Boolean = True
        Private _pinDirectory As String = ""
        Private _pinFile As String = ""
        Private _winShell As Object = Nothing
        Private _winShellNS As Object = Nothing
        Private _winShellFile As Object = Nothing

        Public Event onPinned()
        Public Event onUnPinned()
        Public Event onError(ByVal ErrorMessage As String)

        Public ReadOnly Property IsPinned() As Boolean
            Get
                Return _isPinned
            End Get
        End Property
        Public Sub New(ByVal PinDirectory As String, ByVal PinFile As String)
            _pinDirectory = PinDirectory.Trim()
            _pinFile = PinFile.Trim()

            If InitNameSpace() Then
                If InitPinFile() Then
                    checkPinned()
                End If
            End If
        End Sub
        Private Function InitNameSpace() As Boolean
            Try
                _winShell = CreateObject("Shell.Application")
            Catch ex As Exception
                RaiseEvent onError("Error Initializing Shell")
                Return False
            End Try

            If Not _pinDirectory = "" Then
                If _pinDirectory.Substring(_pinDirectory.Length - 1) = "\" Then
                    _pinDirectory = _pinDirectory.Substring(0, _pinDirectory.Length - 1)
                End If

                Try
                    If Not System.IO.Directory.Exists(_pinDirectory) Then
                        RaiseEvent onError("Pin Directory does not exist")
                        Return False
                    End If
                    _winShellNS = _winShell.Namespace(_pinDirectory.ToString())
                    If _winShellNS Is Nothing Then
                        RaiseEvent onError("Pin Directory Error")
                        Return False
                    End If
                Catch ex As Exception
                    RaiseEvent onError("Pin Directory Error")
                    Return False
                End Try
            Else
                RaiseEvent onError("Pin Directory Error")
                Return False
            End If
            Return True
        End Function
        Private Function InitPinFile() As Boolean
            If Not _pinFile = "" Then
                Try
                    If Not System.IO.File.Exists(_pinDirectory & "\" & _pinFile) Then
                        RaiseEvent onError("Pin File does not exist")
                        Return False
                    End If
                    _winShellFile = _winShellNS.ParseName(_pinFile.ToString())
                    If _winShellFile Is Nothing Then
                        RaiseEvent onError("Pin File Error")
                        Return False
                    End If
                Catch ex As Exception
                    RaiseEvent onError("Pin File Error")
                    Return False
                End Try
            Else
                RaiseEvent onError("Pin File Error")
                Return False
            End If
            Return True
        End Function
        Public Sub checkPinned()
            Try
                For Each verb In _winShellFile.Verbs
                    If verb.Name = "P&in to Start menu" Or verb.Name = "Pin to Start Men&u" Then
                        _isPinned = False
                        RaiseEvent onPinned()
                        Return
                    End If
                Next
            Catch ex As Exception
                RaiseEvent onError("Pin Check Error")
                Return
            End Try
            RaiseEvent onUnPinned()
        End Sub
        Public Sub Pin()
            If _isPinned Then
                RaiseEvent onError("Already Pinned")
                Return
            End If
            Try
                For Each verb In _winShellFile.Verbs
                    If verb.Name = "P&in to Start menu" Or verb.Name = "Pin to Start Men&u" Then
                        verb.DoIt()
                        _isPinned = True
                        RaiseEvent onUnPinned()
                        Exit For
                    End If
                Next
            Catch ex As Exception
                RaiseEvent onError("Pin Check Error")
                Return
            End Try
        End Sub
        Public Sub UnPin()
            If Not _isPinned Then
                RaiseEvent onError("File is not Pinned")
                Return
            End If
            Try
                For Each verb In _winShellFile.Verbs
                    If verb.Name = "Unp&in from Start menu" Or verb.Name = "Unpin from Start Men&u" Then
                        verb.DoIt()
                        _isPinned = False
                        RaiseEvent onPinned()
                        Exit For
                    End If
                Next
            Catch ex As Exception
                RaiseEvent onError("Pin Check Error")
                Return
            End Try
        End Sub

    End Class
#End Region



Example:
Create a new form and paste he code below. Make sure to add a button called "btnPin" to your form.
<pre lang="vb">Public Class Form1
    Private _smPinner As smPinner
    Private Sub btnPin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPin.Click
        If btnPin.Text.ToLower() = "pin" Then
            _smPinner.Pin()
        ElseIf btnPin.Text.ToLower() = "unpin" Then
            _smPinner.UnPin()
        End If
    End Sub
    Private Sub smPinnerError(ByVal errmsg As String)
        MessageBox.Show(errmsg)
    End Sub
    Private Sub smPinnerPin()
        btnPin.Text = "Pin"
    End Sub
    Private Sub smPinnerUnPin()
        btnPin.Text = "UnPin"
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _smPinner = New smPinner("C:\windows\system32", "notepad.exe")
        AddHandler _smPinner.onError, AddressOf smPinnerError
        AddHandler _smPinner.onPinned, AddressOf smPinnerPin
        AddHandler _smPinner.onUnPinned, AddressOf smPinnerUnPin
        _smPinner.checkPinned()
    End Sub
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalbad for so many reasons Pin
SteveKing10-Feb-10 20:33
SteveKing10-Feb-10 20:33 
leave pinning items to the start menu to the user. It's not your app that decides that it is so important that it has to be pinned to the start menu - the user decides that. And I'm sure from your script that the user decides to not pin your app at all.

But luckily, your script only works on English OS. So 90% of windows users are save from your hacks.
GeneralRe: bad for so many reasons Pin
jadon197911-Feb-10 15:04
jadon197911-Feb-10 15:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.