Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get a file's icon? 1

Status
Not open for further replies.

testalucida

Programmer
Jan 4, 2003
2
DE
Hi all,

how can I get the icon that is associated with a file's extension (e.g. the Word-Symbol for .doc files)?
Thanks for any hints

Regards
testalucida
 
Use ExtractIcon / ExtractIconEx API
The ExtractIcon function retrieves a handle to an icon from the specified executable file, dynamic-link library (DLL), or icon file.

Code:
HICON ExtractIcon(
  HINSTANCE hInst,          // instance handle
  LPCTSTR lpszExeFileName,  // filename of file with icon
  UINT nIconIndex           // index of icon to extract
);

ExtractIconEx
The ExtractIconEx function creates an array of handles to large or small icons extracted from the specified executable file, dynamic-link library (DLL), or icon file.
Code:
UINT ExtractIconEx(
  LPCTSTR lpszFile,          
  int nIconIndex,           
  HICON FAR *phiconLarge,  
  HICON FAR *phiconSmall,  
  UINT nIcons               
);
You must destroy all icons extracted by ExtractIcon by calling the DestroyIcon function.

Here is C# code using ExtractIcon():
Code:
//
[DllImport("shell32.dll", CharSet=CharSet.Auto ,SetLastError=True)]
private static extern System.IntPtr ExtractIcon( System.IntPtr hInst, string szExeFileName,int nIndex ) 
private static extern bool DestroyIcon(System.IntPtr hIcon );
private static extern bool DrawIcon(System.IntPtr hDC, System.Int32 X, System.In32 Y, System.IntPtr hIcon);
System.IntPtr hIcon = ExtractIcon(Process.GetCurrentProcess().Handle,"C:\\Program Files\\Microsoft Office\\Office10\\winword.exe" 0);
if (hIcon == IntPtr.Zero)
{
   // extraction error
   return;
}
// process here the hIcon e.g. draw it on the MyForm object using the DrawIcon() api 
DrawIcon(MyForm.hDc,150,125,hIcon);
// Release the handle
DestroyIcon(hIcon);

Note that, Process.GetCurrentProcess().Handle is a handle to your application that calls this code.
Go to to see example in VB which you can translate in C#.

obislavu
 
I think I missunderstood your question.
Giving the file extension e.g. .doc, you can retrieve from the HKEY_CLASSES_ROOT registry the *.exe or *.dll associated with that extension and after that use the ExtractIcon() API as explained above and here.
Having the name of .exe or .dll, the following example is working and extract the first icon for winword.exe:
Code:
 [code]
private System.ComponentModel.Container components = null;
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
private static extern System.IntPtr ExtractIcon( System.IntPtr hInst, string szExeFileName,int nIndex ); 
[DllImport("user32.dll", CharSet=CharSet.Auto )]
private static extern bool DestroyIcon(System.IntPtr hIcon );

//...

private void button1_Click(object sender, System.EventArgs e)
{
	System.IntPtr hIcon = ExtractIcon(Process.GetCurrentProcess().Handle,"C:\\Program Files\\Microsoft Office\\Office10\\winword.exe",0);
	if (hIcon == IntPtr.Zero)
	{
	// extraction error
	return;
	}
	// process here the hIcon e.g. draw it on the MyForm object using the DrawIcon() 
	Graphics newGraphics = Graphics.FromHwnd(this.Handle);
	// Draw rectangle to screen.
	newGraphics.DrawIcon(Icon.FromHandle(hIcon),new Rectangle(0, 0, 100, 75));
	// Dispose of new graphics.
	newGraphics.Dispose();

	// Release the handle
	DestroyIcon(hIcon);

}
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top