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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/343200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:49:02  来源:igfitidea点击:

Extract Icon from Windows .lnk (shortcut) file

windowsiconsshortcut

提问by

I need to extract the icon from a windows shortcut (.lnk) file (or find the icon file, if it's just pointed to by the shortcut).

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

I'm not asking about extracting icons from exe's, dll's, etc. The shortcut in question is created when I run a installation program. And the icon displayed by the shortcut is not contained in the .exe that the shortcut points to. Presumably the icon is embedded in the .lnk file, or the .lnk file contains a pointer to where this icon lives. But none of the utilities I've found work address this -- they all just go to the .exe.

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

Many thanks!

非常感谢!

采纳答案by VonC

This thread provides interesting informations about the data contained in a .lnk file

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

The sSHGetFileInfossfunction should be able to extract the icon file.

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

Documented here, and used for a lnk file:

记录在此处,并用于 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}

From the first link, you could build such an utility in c#, where you would declare this function like:

从第一个链接开始,您可以在 c# 中构建这样一个实用程序,您可以在其中声明此函数,例如:

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


You could also built an utility in autoit script language, where you would use that function declared like this:

您还可以在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

回答by Robbie

Using the Shell32 method of acessing links:

使用 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();

回答by Helge Klein

In 2010 Microsoft finally released an official specificationof the LNK file format. It much more accurate and detailed than the reverse-engineered specs floating around the net, of course.

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

For completeness sake, hereis the MSDN description of shell links and shortcuts.

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

回答by Treb

You can also parse the .lnk file yourself, see this pdfor this articleon the details of the shortcut file format.

也可以自己解析 .lnk 文件,关于快捷方式文件格式的详细信息,请参阅此 pdf此文章

Or you can use the ShellLink class mentioned in the answer to this question.

或者您可以使用此问题的答案中提到的 ShellLink 类。

回答by Chris S

To add a few more resources to this, because presumeably you wan't the Application's icon and not icon that has the shortcut in the bottom left corner:

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

回答by Remon Ramy

I use this to get the icon withoutthe shortcut arrow mini icon added over it as an image:

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

using IWshRuntimeLibrary;

using IWshRuntimeLibrary;

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

If you wish to get it as an icon instead:

如果你想把它作为一个图标来代替:

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