2012-02-26 29 views
2

我發現這個代碼:VB.NET/C#代碼訪問鏈接(LNK)的目標路徑的文件會產生一些錯誤的路徑

Public Shared Function GetLnkTarget(ByVal lnkPath As String) As String 
    Dim shl = New Shell32.Shell() 
    ' Move this to class scope 
    lnkPath = System.IO.Path.GetFullPath(lnkPath) 
    Dim dir = shl.[NameSpace](System.IO.Path.GetDirectoryName(lnkPath)) 
    Dim itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath)) 
    Dim lnk = DirectCast(itm.GetLink, Shell32.ShellLinkObject) 
    Return lnk.Target.Path 
    End Function 

它適用於一些.LNK文件,但例如,如果我加我它產生的Skype.exe桌面鏈接:

C:\Windows\Installer\{...}\SkypeIcon.exe 

是否有修復?

+0

嗯,看起來很像你實際上使用GetIconLocation()方法。 – 2012-02-26 18:14:56

+0

在這種情況下對我的問題意味着什麼?不知道它是如何解釋爲什麼它適用於某些鏈接,但不適用於其他鏈接(例如,它不適用於我的桌面上的Skype鏈接) – user670186 2012-02-27 23:41:15

回答

0

試試這個:

Function GetTargetPath(ByVal FileName As String) 

    Dim Obj As Object 
    Obj = CreateObject("WScript.Shell") 

    Dim Shortcut As Object 
    Shortcut = Obj.CreateShortcut(FileName) 
    GetTargetPath = Shortcut.TargetPath 


End Function 

    Private Sub Teste_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

MsgBox(GetTargetPath("C:\ProgramData\Microsoft\Windows\Start Menu\BitTorrent.lnk")) 

'here you chose the location of .lnk file 

End Sub 
0

CreateShortcut()作爲具有一定的快捷方式出現一個灰色的目標的屬性,如Adobe Reader和Microsoft Word中不正常工作。目標路徑最終成爲c:\ windows \ installer(圖標?)下的東西。