Click here to Skip to main content
15,898,134 members
Articles / Desktop Programming / Windows Forms

Visual Application Launcher

Rate me:
Please Sign up or sign in to vote.
4.91/5 (37 votes)
23 Jan 2012CPOL23 min read 108.2K   3.7K   116  
A WinForms UI using WCF services, Entity Framework, repository data access, repository caching, Unit of Work, Dependency Injection, and every other buzz work you can think of!
namespace VAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Simple class to define some of the information required to display file
    /// information
    /// </summary>
    public class KnownExplorerFile
    {
        /// <summary>
        /// Default ctor
        /// </summary>
        /// <param name="description">The description of the file type</param>
        /// <param name="icon">The icon for the file type</param>
        public KnownExplorerFile(string description, Icon icon)
        {
            this.TypeDescription = description;
            this.Icon = icon;
        }

        /// <summary>
        /// The description of the file as seen in windows explorer
        /// </summary>
        public string TypeDescription { get; private set; }

        /// <summary>
        /// An icon associated with a file type
        /// </summary>
        public Icon Icon { get; private set; }



        /// <summary>
        /// Method that returns information about a particular file
        /// <para>
        /// This will get the file type description (as seen in windows explorer) and the 
        /// icon associated with the file type
        /// </para>
        /// </summary>
        /// <param name="fileName">The filename to retrieve information about</param>
        /// <returns>A <see cref="KnownExplorerFile"/> object</returns>
        public static KnownExplorerFile GetInstance(string fileName)
        {
            NativeMethods.SHFILEINFO shinfo = new NativeMethods.SHFILEINFO();
            IntPtr fi;
            /*
             Type: DWORD_PTR
             Returns a value whose meaning depends on the uFlags parameter.
             If uFlags does not contain SHGFI_EXETYPE or SHGFI_SYSICONINDEX, 
             the return value is nonzero if successful, or zero otherwise.
            */
            fi = NativeMethods.SHGetFileInfo(fileName, 0, ref shinfo,
                             (uint)Marshal.SizeOf(shinfo),
                              SHGetFileInfoFlags.SHGFI_ICON |
                              SHGetFileInfoFlags.SHGFI_TYPENAME);

            KnownExplorerFile knownFile = null;
            if (fi.ToInt32() != 0)
            {
                // Clone the icon from handle, then clean up  
                Icon fileIcon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
                NativeMethods.DestroyIcon(shinfo.hIcon);

                string fileType = shinfo.szTypeName;
                knownFile = new KnownExplorerFile(fileType, fileIcon);
            }
            else
            {
                knownFile = new KnownExplorerFile("Unknown", null);
            }
            return knownFile;
        }

        /// <summary>
        /// Gets the file type description for the specified file
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetFileTypeDescription(string fileName)
        {
            NativeMethods.SHFILEINFO shinfo = new NativeMethods.SHFILEINFO();
            IntPtr fi;
            string fileType = string.Empty;

            fi = NativeMethods.SHGetFileInfo(fileName, 0, ref shinfo,
                             (uint)Marshal.SizeOf(shinfo),
                              SHGetFileInfoFlags.SHGFI_TYPENAME);

            if (fi.ToInt32() != 0)
                fileType = shinfo.szTypeName;

            return fileType;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions