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.
using System;
using System.Collections.Generic;
using System.Text;
using ShellDll;
using System.Runtime.InteropServices;

namespace System.IO
{
    public class Storage : IStorage, IDisposable
    {
        private IStorage _iStorage = null;
        private IntPtr _ptrStorage = IntPtr.Zero;

        internal Storage(IntPtr storagePtr)
        {
            _iStorage = (IStorage)Marshal.GetTypedObjectForIUnknown(storagePtr, typeof(IStorage));
        }

        #region IDisposable Members

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

        public void Dispose()
        {
            if (_iStorage != null)
            {
                Marshal.ReleaseComObject(_iStorage);
                _iStorage = null;
            }

            if (_ptrStorage != IntPtr.Zero)
            {
                Marshal.Release(_ptrStorage);
                _ptrStorage = IntPtr.Zero;
            }

            GC.SuppressFinalize(this);

        }

        #endregion

        #region IStorage Members

        public int CreateStream(string pwcsName, ShellAPI.STGM grfMode, int reserved1, int reserved2, out IntPtr ppstm)
        {
            return _iStorage.CreateStream(pwcsName, grfMode, reserved1, reserved2, out ppstm);
        }

        public int OpenStream(string pwcsName, IntPtr reserved1, ShellAPI.STGM grfMode, int reserved2, out IntPtr ppstm)
        {
            return _iStorage.OpenStream(pwcsName, reserved1, grfMode, reserved2, out ppstm);
        }

        public int CreateStorage(string pwcsName, ShellAPI.STGM grfMode, int reserved1, int reserved2, out IntPtr ppstg)
        {
            return _iStorage.CreateStorage(pwcsName, grfMode, reserved1, reserved2, out ppstg);
        }

        public int OpenStorage(string pwcsName, IStorage pstgPriority, ShellAPI.STGM grfMode, IntPtr snbExclude, int reserved, out IntPtr ppstg)
        {
            return _iStorage.OpenStorage(pwcsName, pstgPriority, grfMode, snbExclude, reserved, out ppstg);
        }

        public int CopyTo(int ciidExclude, ref Guid rgiidExclude, IntPtr snbExclude, IStorage pstgDest)
        {
            return _iStorage.CopyTo(ciidExclude, ref rgiidExclude, snbExclude, pstgDest);
        }

        public int MoveElementTo(string pwcsName, IStorage pstgDest, string pwcsNewName, ShellAPI.STGMOVE grfFlags)
        {
            return _iStorage.MoveElementTo(pwcsName, pstgDest, pwcsName, grfFlags);
        }

        public int Commit(ShellAPI.STGC grfCommitFlags)
        {
            return _iStorage.Commit(grfCommitFlags);
        }

        public int Revert()
        {
            return _iStorage.Revert();
        }

        public int EnumElements(int reserved1, IntPtr reserved2, int reserved3, out IntPtr ppenum)
        {
            return _iStorage.EnumElements(reserved1, reserved2, reserved3, out ppenum);
        }

        public int DestroyElement(string pwcsName)
        {
            return _iStorage.DestroyElement(pwcsName);
        }

        public int RenameElement(string pwcsOldName, string pwcsNewName)
        {
            return _iStorage.RenameElement(pwcsOldName, pwcsNewName);
        }

        public int SetElementTimes(string pwcsName, ShellAPI.FILETIME pctime, ShellAPI.FILETIME patime, ShellAPI.FILETIME pmtime)
        {
            return _iStorage.SetElementTimes(pwcsName, pctime, patime, pmtime);
        }

        public int SetClass(ref Guid clsid)
        {
            return _iStorage.SetClass(ref clsid);
        }

        public int SetStateBits(int grfStateBits, int grfMask)
        {
            return _iStorage.SetStateBits(grfStateBits, grfMask);
        }

        public int Stat(out ShellAPI.STATSTG pstatstg, ShellAPI.STATFLAG grfStatFlag)
        {
            return _iStorage.Stat(out pstatstg, grfStatFlag);
        }

        #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