从Windows .lnk(快捷方式)文件中提取图标

我需要从一个Windows快捷方式(.lnk)文件中提取图标(或者find图标文件,如果只是指向快捷方式)。

我不问从EXE的,DLL等提取图标。有问题的快捷方式创build时,我运行一个安装程序。 并且快捷方式显示的图标不包含在快捷方式指向的.exe文件中。 假定图标embedded在.lnk文件中,或者.lnk文件包含一个指向此图标所在位置的指针。 但是我find的所有工具都没有解决这个问题 – 他们都只是去了.exe。

非常感谢!

此线程提供有关.lnk文件中包含的数据的有趣信息

sSHGetFileInfoss函数应该能够提取图标文件。

记录在这里 ,并用于lnk文件:

Path2Link := 'C:\Stuff\TBear S Saver.lnk'; SHGetFileInfo(PChar(Path2Link), 0, ShInfo1, SizeOf(TSHFILEINFO), SHGFI_ICON); // this ShInfo1.hIcon will have the Icon Handle for the Link Icon with // the small ShortCut arrow added} 

从第一个链接,你可以在C#中建立这样一个实用程序,在那里你会声明这个功能,如:

 [DllImport("shell32.dll")] public static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); 

你也可以用autoit脚本语言构建一个实用程序,在那里你可以使用这个声明:

 Func _ShellGetAssocIcon(Const $szFile,Const $IconFlags = 0) Local $tFileInfo = DllStructCreate($tagSHFILEINFO) If @error Then Return SetError(1,@extended,0) EndIf Local $Ret = DllCall("shell32.dll","int","SHGetFileInfo","str",$szFile,"dword",0, _ "ptr",DllStructGetPtr($tFileInfo),"uint",DllStructGetSize($tFileInfo),"uint",BitOr($SHGFI_ICON,$IconFlags)) MsgBox(0,0,@error) Return DllStructGetData($tFileInfo,"hIcon") EndFunc 

使用Shell32方法访问链接:

 String lnkPath = @"C:\Users\PriceR\Desktop\Microsoft Word 2010.lnk"; //--- run microsoft word var shl = new Shell32.Shell(); // Move this to class scope lnkPath = System.IO.Path.GetFullPath(lnkPath); var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath)); var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath)); var lnk = (Shell32.ShellLinkObject)itm.GetLink; //lnk.GetIconLocation(out strIcon); String strIcon; lnk.GetIconLocation(out strIcon); Icon awIcon = Icon.ExtractAssociatedIcon(strIcon); this.button1.Text = ""; this.button1.Image = awIcon.ToBitmap(); 

您也可以自己解析.lnk文件,看到这个pdf或这篇文章的快捷方式文件格式的细节。

或者你可以使用这个问题的答案中提到的ShellLink类。

在2010年,微软终于发布了LNK文件格式的官方规范 。 当然,它比网络上的反向工程规范更精确,更详细。

为了完整起见,下面是shell链接和快捷方式的MSDN描述。

为此添加更多的资源,因为可以假定你不是应用程序的图标,也不是在左下角有快捷方式的图标:

  • 以下是使用WinApi的IShellLink解析LNK格式的方法
  • 一个IShellLink定义
  • LNK格式,如果你不想使用IShellLink解析它

我用这个来获取没有添加快捷箭头迷你图标的图标作为图像:

using IWshRuntimeLibrary;

 Image ShortcutIcon = System.Drawing.Icon.ExtractAssociatedIcon(((IWshShortcut)new WshShell().CreateShortcut(File)).TargetPath).ToBitmap(); 

如果你想把它作为一个图标,而不是:

 Icon ShortcutIcon = System.Drawing.Icon.ExtractAssociatedIcon(((IWshShortcut)new WshShell().CreateShortcut(File)).TargetPath);