Click here to Skip to main content
15,891,033 members
Articles / Desktop Programming / WPF

A Toy Tabbed File Explorer for Prototype and MVVM Exercise

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Jun 2012CPOL23 min read 29.2K   1.4K   16  
This article describes a Tabbed File Explorer with minimal functionality using only basic MVVM techniques and some attached properties. In a first article I described a MVVM Tabbed Navigation Tree, in this article I add a Tabbed Folderplane.
using System;
using System.Text;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Drawing;     // add to references
using System.Collections.Generic;

// Note this procedure can be used as part of code that: 
// - does some tests on filename so that in specific cases specific icons can be used
// - uses a dictionairy for extensions

namespace Utils
{

    // ImageCache for some speed
    public static class ImageCache
    {
        public static Dictionary<String, BitmapSource> imageList = new Dictionary<String, BitmapSource>();

        public static BitmapSource GetImage(string fullpath)
        {
            string ext = Path.GetExtension(fullpath);
            ext.ToLower();

            // if in the list we are done
            if (imageList.ContainsKey(ext))
            {
                return imageList[ext];
            }

            // get the image
            BitmapSource myIcon;
            myIcon = Utils.GetIconFn.GetIconDll(fullpath);

            // if not individual file, put it in the imageList
            if ((ext != "") && (ext != ".exe") && (ext != ".lnk") && (ext != ".ico"))
            {
                imageList.Add(ext, myIcon);
            }
            return myIcon;
        }
    }

    public static class GetIconFn
    {

        public static System.Windows.Media.Imaging.BitmapSource GetIconDll(string fileName)
        {
            BitmapSource myIcon = null;

            Boolean validDrive = false;
            foreach (DriveInfo D in System.IO.DriveInfo.GetDrives())
            {   //D.DriveType.
                if (fileName == D.Name)
                {
                    validDrive = true;
                }
            }

            if ((System.IO.File.Exists(fileName)) || (System.IO.Directory.Exists(fileName)) || (validDrive))
            {
                using (System.Drawing.Icon sysIcon = ShellIcon.GetLargeIcon(fileName))
                {
                    try
                    {
                        myIcon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                                        sysIcon.Handle,
                                        System.Windows.Int32Rect.Empty,
                                        System.Windows.Media.Imaging.BitmapSizeOptions.FromWidthAndHeight(34, 34));
                    }
                    catch
                    {
                        myIcon = null;
                    }
                }
            }
            return myIcon;
        }
    }
}

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
Netherlands Netherlands
Retired hobby programmer.

Comments and Discussions