2008-12-05 18 views
6

Necesito extraer el icono de un archivo de acceso directo de Windows (.lnk) (o buscar el archivo de icono, si solo lo señala el atajo).Extraiga el icono del archivo .lnk (acceso directo) de Windows

No estoy preguntando sobre la extracción de iconos de exe, dll, etc. El acceso directo en cuestión se crea cuando ejecuto un programa de instalación. Y el ícono que muestra el acceso directo no está contenido en el .exe al que apunta el acceso directo. Presumiblemente, el icono está incrustado en el archivo .lnk, o el archivo .lnk contiene un puntero al lugar donde vive este ícono. Pero ninguna de las utilidades que he encontrado funciona aborda esto, todos simplemente van al .exe.

¡Muchas gracias!

Respuesta

5

El hilo da informaciones interesantes sobre el data contained in a .lnk file

La función sSHGetFileInfoss debe ser capaz de extraer el archivo de icono.

Documentados here, y se utiliza para un archivo 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} 

Desde el primer eslabón, se podría construir una utilidad de este tipo en C#, donde desea declarar esta función como:

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

También podría crear una utilidad en autoit script language, donde usaría esa función declarada así:

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 
+1

Tal vez darle un poco más de información, como el formato struct etc. Pero tienes mi voto. –

1

También puede analizar el archivo .lnk usted mismo, consulte this pdf o this article en los detalles del formato del archivo de acceso directo.

O puede utilizar la clase ShellLink mencionada en la respuesta a this question.

+0

ambos enlaces están muertos. :/ – saint1729

+0

El enlace .PDF está archivado aquí: http://web.archive.org/web/20090408051551/http://www.i2s-lab.com/Papers/The_Windows_Shortcut_File_Format.pdf, y el artículo está archivado aquí: http://web.archive.org/web/20110817051855/http://www.stdlib.com/art6-Shortcut-File-Format-lnk.html –

1

En 2010 Microsoft finalmente lanzó un specification oficial del formato de archivo LNK. Es mucho más preciso y detallado que las especificaciones de ingeniería inversa que flotan en la red, por supuesto.

Para mayor completitud, here es la descripción de MSDN de enlaces de shell y accesos directos.

5

Utilizando el método Shell32 de enlaces acessing:

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(); 
+2

Copias pesadas desde http://stackoverflow.com/a/2566008/17034 –

0

Lo utilizo para obtener el icono sin el acceso directo flecha mini-icono añadió sobre ella como una imagen:

using IWshRuntimeLibrary;

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

Si desea obtenerlo como un icono en su lugar:

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

Este código no funciona para mí. ¿Puedes elaborar? –

Cuestiones relacionadas