Using IShellLink Interface to resolve a shortcut which has changed their drive letter

半城伤御伤魂 提交于 2019-12-09 04:38:41

This is only tested with a connected network drive (i won`t change my local driveletters for testing):

Public Shared Function ResolveShortcut(filename As String,hwnd As IntPtr) As String
    Dim link As New ShellLink()
    DirectCast(link, IPersistFile).Load(filename, STGM_READ)

    DirectCast(link, IShellLinkW).Resolve(hwnd, SLR_FLAGS.SLR_UPDATE)

    Dim sb As New StringBuilder(MAX_PATH)
    Dim data As New WIN32_FIND_DATAW()
    DirectCast(link, IShellLinkW).GetPath(sb, sb.Capacity, data, 0)
    Return sb.ToString()
End Function

To be called like:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim TargetFilename As String = ResolveShortcut("C:\Test.lnk",Me.Handle)
End Sub

As the documentation, you have reffered to, states :

SLR_UPDATE (0x0004)
If the link object has changed, update its path and list of identifiers. If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine whether the link object has changed.

So passing the Handle of the form ( which the Shell will need if it needs to display some dialog ) for calling the Resolve method and passing SLR_UPDATE as second parameter, results for a connected networkdrive in the right path (at least on my site).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!