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