Extract File Icon VB.NET & C#

Okay haven’t posted for so long on my blog, so I had some free minutes and an idea, so I spend that free time I had and made a function which let’s you extract a icon from an file and adds it for you to a ImageList anyway enough writing as you know I’m lazy in that.

Screenshot on how it can look in a listview


Okay this is the VB.NET Snippet

Private intIndex As Integer = 0
Private Function GetIcon(ByVal strFileName As String, ByVal imglName As ImageList) As Integer
GetIcon = Nothing
imglName.ColorDepth = ColorDepth.Depth32Bit
Dim MyIcon As Icon = Icon.ExtractAssociatedIcon(strFileName)
Return intIndex

Catch ex As Exception
MessageBox.Show(ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Function

This is the C# Snippet

private int intIndex = 0;
private int GetIcon(string strFileName, ImageList imglName)
int functionReturnValue = 0;
functionReturnValue = null;
try {
imglName.ColorDepth = ColorDepth.Depth32Bit;
Icon MyIcon = Icon.ExtractAssociatedIcon(strFileName);
return intIndex;

} catch (Exception ex) {
MessageBox.Show(ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);
return functionReturnValue;

and this way you should call it from your code for example it’s a nasty one.

ListView1.Items.Add(OpenFileDialog1.FileName, GetIcon(OpenFileDialog1.FileName, ImageList1))
intIndex = intIndex + 1

ugly ain’t it? anyway as usual i welcome any kind of suggestion or comment’s to this post.

thanks in advanced for suggestions or ideas.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s