8

我需要从 Windows 快捷方式 (.lnk) 文件中提取图标(或找到图标文件,如果它只是由快捷方式指向的话)。

我不是要从 exe、dll 等中提取图标。有问题的快捷方式是在我运行安装程序时创建的。并且快捷方式显示的图标不包含在快捷方式指向的.exe中。大概图标嵌入在 .lnk 文件中,或者 .lnk 文件包含指向该图标所在位置的指针。但是我找到的实用程序都没有解决这个问题——它们都只是转到.exe。

非常感谢!

4

6 回答 6

7

使用 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();
于 2012-11-26T23:31:37.717 回答
6

该线程提供了有关.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
于 2008-12-05T08:36:18.013 回答
2

2010 年微软终于发布了 LNK 文件格式的官方规范。当然,它比网上漂浮的逆向工程规范要准确和详细得多。

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

于 2011-08-26T11:43:04.153 回答
1

也可以自己解析 .lnk 文件,有关快捷文件格式的详细信息,请参见此pdf本文

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

于 2008-12-05T09:13:20.293 回答
1

我使用它来获取没有在其上添加快捷箭头迷你图标作为图像的图标:

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);
于 2017-01-04T01:51:57.177 回答
0

要为此添加更多资源,因为大概您不需要应用程序的图标而不是左下角有快捷方式的图标:

于 2009-09-14T10:59:39.520 回答