Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Pin items to the Start Menu

, 10 Feb 2010
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

jadon1979

United States United States
No Biography provided

Comments and Discussions

 
Generalbad for so many reasons PinmemberSteveKing10-Feb-10 20:33 
GeneralRe: bad for so many reasons Pinmemberjadon197911-Feb-10 15:04 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 10 Feb 2010
Article Copyright 2010 by jadon1979
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid