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

发布于 2024-07-09 16:23:32 字数 249 浏览 7 评论 0原文

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

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

非常感谢!

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).

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.

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

彼岸花ソ最美的依靠 2024-07-16 16:23:33

您还可以自己解析 .lnk 文件,请参阅此 pdf这篇文章介绍了快捷方式文件格式的详细信息。

或者您可以使用 这个问题

You can also parse the .lnk file yourself, see this pdf or this article on the details of the shortcut file format.

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

美人迟暮 2024-07-16 16:23:33

我使用它来获取图标而不将快捷箭头迷你图标作为图像添加到其上:

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

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

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);
谎言月老 2024-07-16 16:23:33

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

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:

情绪操控生活 2024-07-16 16:23:32

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

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

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

The sSHGetFileInfoss function should be able to extract the icon file.

Documented here, and used for a lnk file:

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:

[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:

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
够钟 2024-07-16 16:23:32

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

Using the Shell32 method of acessing links:

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();
脸赞 2024-07-16 16:23:32

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文