using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SystemIconDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            #if DEBUG
                MinimizeBox = false;
            #else
                MinimizeBox = true;
            #endif
        }

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

        [StructLayout(LayoutKind.Sequential)]
        struct SHFILEINFO
        {
            public IntPtr hIcon;
            public IntPtr iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        }

        const uint SHGFI_ICON = 0x000000100;
        const uint SHGFI_LARGEICON = 0x000000000;
        const uint SHGFI_SMALLICON = 0x000000001;
        const uint SHGFI_SELECTICON = 0x000040000;

        Icon GetSmallIcon(string file)
        {
            SHFILEINFO shinfo = new SHFILEINFO();
            IntPtr i = SHGetFileInfo(file, 0, ref shinfo,
              (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);

            return Icon.FromHandle(shinfo.hIcon);
        }

        Icon GetLargeIcon(string file)
        {
            SHFILEINFO shinfo = new SHFILEINFO();
            IntPtr i = SHGetFileInfo(file, 0, ref shinfo,
              (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);

            return Icon.FromHandle(shinfo.hIcon);
        }

        private void butGetIcon_Click(object sender, EventArgs e)
        {
            Icon ico;
            ico = GetLargeIcon(@"\windows\bubblebreaker.exe");
            Graphics g = CreateGraphics();
            g.DrawIcon(ico, 10, 10);
            g.Dispose();
        }
    }
}