2012-10-25 44 views
1

我試着去使用此功能,但得到的一些錯誤:我如何獲得目標文件在我的硬盤上的快捷方式?

public 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)); 
     } 

第一個錯誤是上線:

Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; 

上線(Shell32.ShellLinkObject)的右手folderItem.GetLink 即時得到錯誤:

Error 2 One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll? 

而就在最後一行:

Console.WriteLine(GetShortcutTargetFile(path)); 

錯誤是在:GetShortcutTargetFile(路徑)函數是靜態的,但我刪除了靜態,然後我在最後一行mgetting錯誤。

Error 4 An object reference is required for the non-static field, method, or property 'GatherLinks.Form1.GetShortcutTargetFile(string) 

我該如何解決所有的錯誤以及如何獲得所有的目標文件的短路?

+0

這個想法是知道讀取一個快捷方式文件。例如,把它們全部放在List中。 – user1741587

+1

谷歌可以很好地解決這兩個錯誤... – TGlatzer

回答

2

第一個錯誤:在項目設置中添加對Shell32.dll的引用。

第二個錯誤:你在哪裏放置這兩個函數?看起來你正在嘗試在表單中創建函數。這就是爲什麼你不能訪問「GatherLinks.Form1.GetShortcutTargetFile(string)」。 將你的代碼從主函數移動到Form加載事件,你將能夠編譯:)

相關問題