Posts from March 2008

Mar
26
2008

Priority Queue in C#



Posted in Programming

A priority queue is like a standard queue, but instead of dequeuing items on a first-in-first-out basis, items are instead served by priority. There are lots of implementations of a priority queue, but none I could find which let the user re-order items. The following class functions just like a regular queue, but can also change the order of items in the queue by their index.

The queue is designed to be used with a ListView set to details mode, but could be adapted for use elsewhere. Using it with a ListView makes sense since the first item in the queue (the next to be executed) is always at index 0, and the indexing for a ListView also starts at 0.

Read more »

Mar
23
2008

Retrieving Shell Icons in C#



Posted in Programming

Ever needed to retrieve the shell icon for a particular file type in C#? This class will retrieve the Icon associated with a file from just the extension.

using System;
using System.Runtime.InteropServices;
using System.Drawing;
 
namespace Lime49.Utils {
    /// <summary>
    /// Retrievs shell info associated with a file or filetype
    /// </summary>
    /// <summary>
    /// Get a 32x32 or 16x16 System.Drawing.Icon depending on which function you call
    /// either GetSmallIcon(string fileName) or GetLargeIcon(string fileName)
    /// </summary>
    public class ShellIcon {
        [StructLayout(LayoutKind.Sequential)]
        public 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;
        };
        class Win32 {
            public const uint SHGFI_ICON = 0x100;
            public const uint SHGFI_LARGEICON = 0x0; // Large icon
            public const uint SHGFI_SMALLICON = 0x1; // Small icon
            public const uint USEFILEATTRIBUTES = 0x000000010; // when the full path isn't available
            [DllImport("shell32.dll")]
            public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
            [DllImport("User32.dll")]
            public static extern int DestroyIcon(IntPtr hIcon);
        }
	...

Read more »