Results 1 to 4 of 4

Thread: Extract the Icon Associated with any File on Your System

  1. #1

    Thread Starter
    New Member WidgetMan's Avatar
    Join Date
    Feb 2005
    Posts
    10

    Extract the Icon Associated with any File on Your System

    Contained in the zip file are two implementations, in both VB.NET and C#, of an "IconExtractor" class that allows you to obtain the icon associated with any file on your system. The implementation tucks away all the Win32 API stuff into a nice black box. You call the simple "Extract" method and pass it the file name and the desired icon size.
    Attached Files Attached Files

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Extract the Icon Associated with any File on Your System

    Nice and simple.
    I don't live here any more.

  3. #3

    Thread Starter
    New Member WidgetMan's Avatar
    Join Date
    Feb 2005
    Posts
    10

    Re: Extract the Icon Associated with any File on Your System

    Thanks Man! I got a real kick out of your profile, by-the-way. Very funny stuff!
    WidgetMan
    http://softwidgets.com

    "You know that developing software has become an art for you when you get into the zone and, in a ten hour time span, you only get out of your chair once or twice to hydrate and/or caffeinate and relieve yourself."

  4. #4
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Extract the Icon Associated with any File on Your System

    This will crash if you send it a file that doesn't exist. To get the icon for "x.txt" where x.txt doesn't actually exists, add this to the top.
    Code:
    Private Const SHGFI_USEFILEATTRIBUTES = &H10
    And add this to Extract()
    Code:
            Select Case Size
                Case IconSize.Large
                    uflags = SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_USEFILEATTRIBUTES
                Case Else
                    uflags = SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_USEFILEATTRIBUTES
            End Select
    For what I was doing, I actually needed to dynamically grab the icon for a folder. This won't crash, but it won't return the folder icon. The original code will. I got around this by sending Extract and optional parameter that was set to 1 by default.

    Note: to use this don't make the above change to the Select Case
    Code:
    Public Overloads Function Extract(ByVal File As String, ByVal Size As IconSize, Optional ByVal UseFileAttributes As Integer = 1) As System.Drawing.Icon
    .
    .
    .
    'directly after the select case
    If UseFileAttributes Then uflags = uflags Or SHGFI_USEFILEATTRIBUTES
    EDIT:
    WidgetMan, thanks a ton for this. It was very easy to use, and very helpful.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width