Click here to Skip to main content
15,886,362 members
Articles / Web Development / HTML

WPF x FileExplorer x MVVM

Rate me:
Please Sign up or sign in to vote.
4.99/5 (52 votes)
24 Nov 2012LGPL323 min read 289.1K   9.4K   228  
This article describe how to construct FileExplorer controls included DirectoryTree and FileList, using Model-View-ViewModel (MVVM) pattern.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LYCJ (c) 2009 - http://www.quickzip.org/components                                                            //
// Release under LGPL license.                                                                                   //
//                                                                                                               //
// This code used part of Steven Roebert's work (http://www.codeproject.com/KB/miscctrl/FileBrowser.aspx)    //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using ShellDll;
using System.Runtime.InteropServices;

namespace System.IO
{
    public class ShellFolder : IShellFolder, IDisposable
    {
        private IShellFolder _iShellFolder = null;
        private IntPtr _ptrShellFolder = IntPtr.Zero;
        private bool _disposed = false;

        internal ShellFolder(IntPtr ptrShellFolder)
        {
            _ptrShellFolder = ptrShellFolder;
            _iShellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(ptrShellFolder, typeof(IShellFolder));
        }

        private void checkDisposed()
        {
            if (_disposed) throw new Exception("ShellFolder already disposed");
        }

        #region IShellFolder Members

        public int ParseDisplayName(IntPtr hwnd, IntPtr pbc, string pszDisplayName, ref uint pchEaten, out IntPtr ppidl, ref ShellAPI.SFGAO pdwAttributes)
        {
            checkDisposed();
            return _iShellFolder.ParseDisplayName(hwnd, pbc, pszDisplayName, ref pchEaten, out ppidl, ref pdwAttributes);
        }

        public int EnumObjects(IntPtr hwnd, ShellAPI.SHCONTF grfFlags, out IntPtr enumIDList)
        {
            checkDisposed();
            return _iShellFolder.EnumObjects(hwnd, grfFlags, out enumIDList);
        }

        public int BindToObject(IntPtr pidl, IntPtr pbc, ref Guid riid, out IntPtr ppv)
        {
            checkDisposed();
            return _iShellFolder.BindToObject(pidl, pbc, ref riid, out ppv);
        }

        public int BindToStorage(IntPtr pidl, IntPtr pbc, ref Guid riid, out IntPtr ppv)
        {
            checkDisposed();
            return _iShellFolder.BindToStorage(pidl, pbc, ref riid, out ppv);
        }

        public int CompareIDs(IntPtr lParam, IntPtr pidl1, IntPtr pidl2)
        {
            checkDisposed();
            return _iShellFolder.CompareIDs(lParam, pidl1, pidl2);
        }

        public int CreateViewObject(IntPtr hwndOwner, Guid riid, out IntPtr ppv)
        {
            checkDisposed();
            return _iShellFolder.CreateViewObject(hwndOwner, riid, out ppv);
        }

        public int GetAttributesOf(uint cidl, IntPtr[] apidl, ref ShellAPI.SFGAO rgfInOut)
        {
            checkDisposed();
            return _iShellFolder.GetAttributesOf(cidl, apidl, ref rgfInOut);
        }

        public int GetUIObjectOf(IntPtr hwndOwner, uint cidl, IntPtr[] apidl, ref Guid riid, IntPtr rgfReserved, out IntPtr ppv)
        {
            checkDisposed();
            return _iShellFolder.GetUIObjectOf(hwndOwner, cidl, apidl, ref riid, rgfReserved, out ppv);
        }

        public int GetDisplayNameOf(IntPtr pidl, ShellAPI.SHGNO uFlags, IntPtr lpName)
        {
            checkDisposed();
            return _iShellFolder.GetDisplayNameOf(pidl, uFlags, lpName);
        }

        public int SetNameOf(IntPtr hwnd, IntPtr pidl, string pszName, ShellAPI.SHGNO uFlags, out IntPtr ppidlOut)
        {
            checkDisposed();
            return _iShellFolder.SetNameOf(hwnd, pidl, pszName, uFlags, out ppidlOut);
        }

        #endregion

        #region IDisposable Members

        ~ShellFolder()
        {
            ((IDisposable)this).Dispose();
        }

        public void Dispose()
        {
            _disposed = true;
            if (_iShellFolder != null)
            {
                Marshal.ReleaseComObject(_iShellFolder);
                _iShellFolder = null;
            }

            if (_ptrShellFolder != IntPtr.Zero)
            {
                try
                {
                    Marshal.Release(_ptrShellFolder);
                }
                catch (Exception) { }
                finally
                {
                    _ptrShellFolder = IntPtr.Zero;
                }
            }

            GC.SuppressFinalize(this);
        }

        #endregion
    }
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Founder
Hong Kong Hong Kong

Comments and Discussions