我需要从Windows快捷方式(.lnk)文件中提取图标(或找到图标文件,如果它只是由快捷方式指向).
我不是要求从exe,dll等中提取图标.当我运行安装程序时,会创建有问题的快捷方式.并且快捷方式显示的图标不包含在快捷方式指向的.exe中.据推测,该图标嵌入在.lnk文件中,或者.lnk文件包含指向此图标所在位置的指针.但是我找到的所有工具都没有解决这个问题 - 他们都只是去了.exe.
非常感谢!
小智 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();
Run Code Online (Sandbox Code Playgroud)
该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}
Run Code Online (Sandbox Code Playgroud)
从第一个链接开始,您可以在c#中构建这样的实用程序,您可以在其中声明此函数:
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(
string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi,
uint cbSizeFileInfo, uint uFlags);
Run Code Online (Sandbox Code Playgroud)
您还可以使用自动脚本语言构建一个实用程序,您可以在其中使用如下所示的函数:
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
Run Code Online (Sandbox Code Playgroud)