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

My Personal Commander Variant

Rate me:
Please Sign up or sign in to vote.
4.66/5 (13 votes)
5 Oct 2016CPOL14 min read 44.5K   1.2K   47  
A C#/WPF application for displaying folders on a grid and performing combined functions on them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace Lobster.FolderBrowser
{
    [System.ComponentModel.Composition.Export(typeof(IPathItemModel))]
    public class FolderFolderModel : FolderItemModelBase
    {
        public FolderFolderModel()
            : base(null, null)
        {
        }
        public FolderFolderModel(string _Pathname, FolderModelConfiguration conf)
            : base(_Pathname, conf)
        {
        }

        public override void UpdateDetails()
        {
            Debug.Assert(Directory.Exists(pathname));

            FileSize = long.MinValue; // Signals the ui the filesize will be shown as an empty string

            DirectoryInfo di = new DirectoryInfo(pathname);
            Attributes = di.Attributes;
            CreationTime = di.CreationTime;
            ChangeTime = di.LastWriteTime;

            if ((configuration != null) && (configuration.CalculateFolderSizes))
            {
                Task t = new Task(() =>
                {
                    var r = GetFolderSize(pathname);
                    long l = r.Item2 ? -r.Item1 : r.Item1;
                    if ((r.Item1 == 0) && (r.Item2 == true)) l = long.MaxValue;

                    FileSize = l;
                });

                t.Start();
            }
        }

        // see http://stackoverflow.com/questions/468119/whats-the-best-way-to-calculate-the-size-of-a-directory-in-net
        private static Tuple<long, bool> GetFolderSize(string folder)
        {
            long folderSize = 0;
            bool isIncomplete = false;
            try
            {
                if (!Directory.Exists(folder)) return new Tuple<long, bool>(0, true);

                try
                {
                    foreach (string file in Directory.GetFiles(folder))
                    {
                        if (File.Exists(file))
                        {
                            FileInfo finfo = new FileInfo(file);
                            folderSize += finfo.Length;
                        }
                    }

                    foreach (string dir in Directory.GetDirectories(folder))
                    {
                        var r = GetFolderSize(dir);
                        folderSize += r.Item1;
                        isIncomplete |= r.Item2;
                    }
                }
                catch (Exception)
                {
                    isIncomplete = true;
                }
            }
            catch (Exception)
            {
                isIncomplete = true;
            }
            return new Tuple<long, bool>(folderSize, isIncomplete);
        }

        public override string NameID { get { return "Folder"; } }
    }
}

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
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions