Click here to Skip to main content
15,892,809 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 125.3K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CBR.Core.Helpers;
using System.Collections.ObjectModel;
using System.IO;

namespace CBR.ViewModels
{
    #region ----------------BASE CLASS----------------

    public enum SysElementType
    {
        Drive,
        Folder,
        File
    }

    public class SysElementViewModel : TreeViewItemViewModel
    {
        public SysElementViewModel(TreeViewItemViewModel parent, string fullPath, string name, DateTime access)
            : base(parent, true)
        {
            FullPath = fullPath;
            Name = name;
            LastModified = access;
        }

        /// <summary>
        /// Display name
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Display type
        /// </summary>
        public SysElementType Type
        {
            get;
            set;
        }

        /// <summary>
        /// last modification date
        /// </summary>
        public DateTime LastModified
        {
            get;
            set;
        }

        /// <summary>
        /// Complete path to file sys object
        /// </summary>
        public string FullPath
        {
            get { return Data as string; }
            set { Data = value; }
        }

        public List<ListSysObjectViewModel> GetListContent()
        {
            List<ListSysObjectViewModel> result = new List<ListSysObjectViewModel>();
            
            foreach (string directory in Directory.GetDirectories(FullPath, "*.*", SearchOption.TopDirectoryOnly))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(directory);
                result.Add(new ListSysDirectoryViewModel(directory, directoryInfo.Name, directoryInfo.LastWriteTime));
            }
            foreach (string file in Directory.GetFiles(FullPath))
            {
                FileInfo fileInfo = new FileInfo(file);
                result.Add(new ListSysFileViewModel(file, fileInfo.Name, fileInfo.LastWriteTime, fileInfo.Length));
            }
            return result;
        }
    }
    #endregion

    #region ----------------FILE----------------

    public class SysFileViewModel : SysElementViewModel
    {
        public SysFileViewModel(TreeViewItemViewModel parent, string fullPath, string name, DateTime access, long size)
            : base(parent, fullPath, name, access)
        {
            Type = SysElementType.File;
            Size = size;
        }

        /// <summary>
        /// file size
        /// </summary>
        public long Size
        {
            get;
            set;
        }
    }

    #endregion

    #region ----------------FOLDER----------------

    public class SysDirectoryViewModel : SysElementViewModel
    {
        public SysDirectoryViewModel(TreeViewItemViewModel parent, string fullPath, string name, DateTime access)
            : base(parent, fullPath, name, access)
        {
            Type = SysElementType.Folder;
        }

        protected override void LoadChildren()
        {
            foreach (string directory in Directory.GetDirectories(FullPath, "*.*", SearchOption.TopDirectoryOnly))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(directory);
                Children.Add(new SysDirectoryViewModel( this, directory, directoryInfo.Name, directoryInfo.LastWriteTime));
            }
            //foreach (string file in Directory.GetFiles(FullPath))
            //{
            //    FileInfo fileInfo = new FileInfo(file);
            //    Children.Add(new SysFileViewModel(this, file, fileInfo.Name, fileInfo.LastWriteTime, fileInfo.Length));
            //}
        }
    }
    #endregion

    #region ----------------DRIVE----------------

    public class SysDriveViewModel : SysElementViewModel
    {
        public SysDriveViewModel(string fullPath)
            : base(null, fullPath, fullPath, DateTime.Now)
        {
            Type = SysElementType.Drive;
        }

        protected override void LoadChildren()
        {
            foreach (string directory in Directory.GetDirectories(FullPath, "*.*", SearchOption.TopDirectoryOnly))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(directory);
                Children.Add(new SysDirectoryViewModel(this, directory, directoryInfo.Name, directoryInfo.LastWriteTime ));
            }
            //foreach (string file in Directory.GetFiles(FullPath))
            //{
            //    FileInfo fileInfo = new FileInfo(file);
            //    Children.Add(new SysFileViewModel(this, file, fileInfo.Name, fileInfo.LastWriteTime, fileInfo.Length));
            //}
        }
    }
    
    #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 General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions