How to: get File icon of some file in windows Explorer

Posted on 2006-04-21
Last Modified: 2010-04-23
For example, how can I get the icon of the notepad file, such as somefile.txt. I want to get the icon image and assign it to a ListViewItem. I am writing a windows explorer emulator.
Something likes this:

Dim x as Bitmap = new Bitmap(GetIconImage("C:\somefile.txt"))
TreeViewItem1.ImageIndex = ImageList1.Items.Count - 1
Question by:maidinhtai
    LVL 96

    Accepted Solution

    LVL 3

    Author Comment

    When I try running the sample in the link, I receive this error:
    Private Sub frmIconViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Const OFFSET As Integer = 20

            Dim extractor As New IconExtractor
            Dim listIcons() As Icon = extractor.ExtractIcons("c:\windows\system32\progman.exe", App.AppInstance) 'This is the line I receive the error:Error      3      Name 'App' is not declared.
    I don't know what App means? Could you explain!
    LVL 96

    Expert Comment

    by:Bob Learned
    Imports System.Reflection
    Imports System.Runtime.InteropServices

    Public Class App

      Public Shared ReadOnly Property AppInstance() As IntPtr
           Return Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly().GetModules()(0))
        End Get
      End Property  'AppInstance

    End Class

    LVL 3

    Author Comment

    It's so great. My problem is almost solved. There's just a small thing more: how can I get the application path of the appliction that used to open the file. For example, I have a file "C:\sample.txt". The application path of the file opening that file is "%SystemRoot%\system32\notepad.exe". What I want now is getting that application path when I have the string "C:\sample.txt".
    Thanks a lot.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now