2012-02-23 79 views
14

如何獲取快捷文件夾的目錄目標?我到處搜索,只能找到快捷方式文件的目標。獲取快捷文件夾的目標

+1

這並不涉及到所有的WPF,我刪除了標籤 – 2012-02-23 13:41:12

+0

這是解決方案我更喜歡:[如何從文件快捷方式獲取路徑名?](http://stackoverflow.com/a/13079688/3757210) – ilcorvo 2017-02-23 13:20:55

回答

22

我想你會需要使用COM,並添加到「微軟殼牌控制和自動化」的引用,如this blog post描述:

下面是使用提供的代碼有一個例子:

namespace Shortcut 
{ 
    using System; 
    using System.Diagnostics; 
    using System.IO; 
    using Shell32; 

    class Program 
    { 
     public static string GetShortcutTargetFile(string shortcutFilename) 
     { 
      string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename); 
      string filenameOnly = System.IO.Path.GetFileName(shortcutFilename); 

      Shell shell = new Shell(); 
      Folder folder = shell.NameSpace(pathOnly); 
      FolderItem folderItem = folder.ParseName(filenameOnly); 
      if (folderItem != null) 
      { 
       Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; 
       return link.Path; 
      } 

      return string.Empty; 
     } 

     static void Main(string[] args) 
     { 
      const string path = @"C:\link to foobar.lnk"; 
      Console.WriteLine(GetShortcutTargetFile(path)); 
     } 
    } 
} 
+0

謝謝!現在我只需要找出如何尋找一個shortcuf文件夾programmaticaly :) – 2012-02-24 12:11:04

+2

我工作在Windows 7 64位,並且它找不到安裝在'C:\ Program Files \ Oracle \ VirtualBoxOSX'上的VirtualBox快捷方式。它一直將目標返回到'C:\ Program Files文件(x86)\ Oracle \ VirtualBoxOSX \ VirtualBox.exe'。對此行爲的任何解釋? – swdev 2014-03-11 01:08:44

+0

在此方法中使用Shell32只允許在單線程應用程序中使用。你必須在主入口點包含[STAThread]。使用此代碼您將在單元測試中遇到異常,因爲單元測試不允許STAThread。請參閱[答案](http://stackoverflow.com/questions/14543340/calling-shell32-dl​​l-from-net-windows-service)。也有解決方案在單元測試中運行此代碼,請參閱http://haacked.com/archive/2014/11/20/xunit-and-sta/ – yancyn 2016-08-19 05:50:44

-2

所有文件快捷方式都有一個.lnk文件擴展名,您可以檢查。例如,使用字符串,你可以使用string.EndsWith(「。lnk」)作爲過濾器。

所有URL快捷方式都有.url文件擴展名,因此如果需要,您還需要考慮這些擴展名。

+1

OP詢問如何獲取目標目錄的快捷方式,而不是如何檢查它是否是一條捷徑。 – 2017-01-25 02:47:33

0

,如果你想找到一個具有快捷方式在桌面上,我用一個簡單的方法你的應用程序的路徑,如下所示:

Process.GetCurrentProcess().MainModule.FileName.Substring(0, Process.GetCurrentProcess().MainModule.FileName.LastIndexOf("\\") 

此代碼返回運行,不管是誰要求文件的任何exe文件路徑

1

一個更簡單的方式來獲得,我用的是鏈接路徑:

private static string LnkToFile(string fileLink) 
{ 
    string link = File.ReadAllText(fileLink); 
    int i1 = link.IndexOf("DATA\0"); 
    if (i1 < 0) 
     return null; 
    i1 += 5; 
    int i2 = link.IndexOf("\0", i1); 
    if (i2 < 0) 
     return link.Substring(i1); 
    else 
     return link.Substring(i1, i2 - i1); 
} 

但它當然會破的,如果LNK文件格式的變化。

2

在它需要這樣做,首先添加COM參考Windows 10「微軟殼牌控制和自動化」

// new way for windows 10 
string targetname; 
string pathOnly = System.IO.Path.GetDirectoryName(LnkFileName); 
string filenameOnly = System.IO.Path.GetFileName(LnkFileName); 

Shell shell = new Shell(); 
Shell32.Folder folder = shell.NameSpace(pathOnly); 
FolderItem folderItem = folder.ParseName(filenameOnly); 
if (folderItem != null) { 
    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; 
    targetname = link.Target.Path; // <-- main difference 
    if (targetname.StartsWith("{")) { // it is prefixed with {54A35DE2-guid-for-program-files-x86-QZ32BP4} 
    int endguid = targetname.IndexOf("}"); 
    if (endguid > 0) { 
     targetname = "C:\\program files (x86)" + targetname.Substring(endguid + 1); 
    } 
} 
相關問題