Pin to Start Menu in Vista
With xp it's easy to pin items to the start menu, by simply adding a com reference to "Windows script host object model"
Pin
Code:
CreateObject("Shell.Application").Namespace(Environment.SystemDirectory).ParseName(fileName_Ext).InvokeVerb("P&in to Start Menu")
Unpin
Code:
CreateObject("Shell.Application").Namespace(Environment.SystemDirectory).ParseName(fileName_Ext).InvokeVerb("Unp&in from Start Menu")
EDIT: This example looks in the system32 folder for the file name specified as fileName_Ext.
However, this doesn't seem to work on Vista.
Does anyone know if this is possible with Vista?
Re: Pin to Start Menu in Vista
I turned up the strictness of the project, and found "late binding" errors.
I don't understand why though.
These are all objects, and they are dimensioned as objects.
How do you resolve the late binding issue with this example?
Re: Pin to Start Menu in Vista
Because a system.object has no properties or methods, so if you try and calls any, then you'll get a late binding error. So, although CreateObject returns an Object, it may not have a namespace property. That's what causes the late binding error.
I can't see a solution immediately, apart from referencing shell32.dll from windows. But that would lead to cross system compatibility issues anyway!
Re: Pin to Start Menu in Vista
That's what I've been thinking lately.
I've tried it several different ways, and still nothing.
I can't be sure that this is why it does'nt work on Vista, but for now it's the only reason I could come up with.
Thanks alot for the confirmation.
Re: Pin to Start Menu in Vista
After some more searching, and some reworking I found a vb script works on Vista.
So the a full solution to both xp and Vista would be something like this for .NET:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fName As String = Process.GetCurrentProcess.ProcessName
Dim pinned As Boolean
pinned = PinToStartMenu(fName, True)
If pinned = True Then
MessageBox.Show("Pinned to start menu")
Else
MessageBox.Show("Not pinned to start menu")
End If
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Dim fName As String = Process.GetCurrentProcess.ProcessName
PinToStartMenu(fName, False)
End Sub
Private Function PinToStartMenu(ByVal fName As String, Optional ByVal pMenu As Boolean = True) As Boolean
On Error Resume Next
CreateShortCut(Environment.CurrentDirectory & "\" & fName & ".exe", Environment.CurrentDirectory, fName)
If pMenu = True Then PinToStartMenu = DoVerb(fName, "P&in to Start Menu") 'P&in
If pMenu = False Then PinToStartMenu = DoVerb(fName, "Unp&in from Start Menu") 'Unp&in
End Function
Private Function CreateShortCut(ByVal filePathOrigin As String, ByVal folderPathDestination As String, ByRef linkName As String) As Boolean
On Error Resume Next
Dim WshShell As Object
Dim oShellLink As Object
WshShell = CreateObject("WScript.Shell")
oShellLink = WshShell.CreateShortCut(folderPathDestination & "\" & linkName & ".lnk")
oShellLink.TargetPath = filePathOrigin
oShellLink.WindowStyle = 1
oShellLink.Hotkey = ""
oShellLink.Description = ""
oShellLink.WorkingDirectory = Environment.CurrentDirectory
oShellLink.Save()
End Function
Private Function DoVerb(ByVal fName As String, ByVal sVerb As String) As Boolean
Dim objShell As Object = CreateObject("Shell.Application")
Dim objFolder As Object = objShell.Namespace(CurDir)
If objFolder Is Nothing Then GoTo Release
Dim objFolderItem As Object = objFolder.ParseName(fName & ".lnk")
If objFolderItem Is Nothing Then GoTo Release
Dim objVerb As Object
Dim objVerbs As Object
Dim osv As String
osv = Environment.OSVersion.VersionString.Substring(21, 3)
If osv = "5.0" OrElse osv = "5.1" Then
If sVerb = "P&in to Start Menu" Then objFolderItem.InvokeVerb("P&in to Start Menu")
If sVerb = "Unp&in from Start Menu" Then objFolderItem.InvokeVerb("Unp&in from Start Menu")
DoVerb = True 'Did it, or at least tried to do it
ElseIf osv.Substring(0, 1) = "6" OrElse osv.Substring(0, 1) = "7" Then
objVerbs = objFolderItem.Verbs
For Each objVerb In objVerbs
If objVerb.Name = sVerb Then objVerb.doit() : DoVerb = True
Next
End If
Release:
objShell = Nothing : objFolder = Nothing : objFolderItem = Nothing : objVerb = Nothing : objVerbs = Nothing
End Function
End Class
You should not always pin executables directly to the start menu, because not all of them will load correctly for some unknown reason.
The solution is to make a shortcut to the executable, and then pin that file(.lnk) to the start menu.
You may want to hide the shortcuts somewhere though, in the case the user deletes the shortcut.