Click here to Skip to main content
15,886,795 members
Articles / Programming Languages / C#

C# File Browser

Rate me:
Please Sign up or sign in to vote.
4.93/5 (173 votes)
21 Aug 200628 min read 2.4M   81.5K   587  
A file browser written in C#, very much like Windows Explorer.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FileBrowser;
using ShellDll;
using System.IO;

namespace BrowserPlugins
{
    public class DefaultColumnPlugin : IColumnPlugin
    {
        private string[] columns = new string[]
            {
                "Size",
                "Date Created",
                "Date Modified"
            };

        #region IColumnPlugin Members

        public string[] ColumnNames
        {
            get { return columns; }
        }

        public string GetFileInfo(IFileInfoProvider provider, string columnName, ShellItem item)
        {
            string retVal = string.Empty;
            ShellAPI.STATSTG info = provider.GetFileInfo();

            switch (columnName)
            {
                case "Size":
                    #region Size
                    {
                        retVal = GetSizeString(info.cbSize);
                    }
                    #endregion
                    break;

                case "Date Created":
                    #region Date Created
                    {
                        DateTime dateTime = ShellAPI.FileTimeToDateTime(info.ctime);
                        string time = dateTime.ToLongTimeString();
                        retVal = string.Format("{0} {1}",
                            dateTime.ToShortDateString(),
                            time.Remove(time.Length - 3, 3));
                    }
                    #endregion
                    break;

                case "Date Modified":
                    #region Date Modified
                    {
                        DateTime dateTime = ShellAPI.FileTimeToDateTime(info.mtime);
                        string time = dateTime.ToLongTimeString();
                        retVal = string.Format("{0} {1}",
                            dateTime.ToShortDateString(),
                            time.Remove(time.Length - 3, 3));
                    }
                    #endregion
                    break;
            }

            return retVal;
        }

        public string GetFolderInfo(IDirInfoProvider provider, string columnName, ShellItem item)
        {
            string retVal = string.Empty;

            if (columnName != "Size" && !item.IsSystemFolder && !item.IsDisk && item.IsFileSystem)
            {
                ShellAPI.STATSTG info = provider.GetDirInfo();

                switch (columnName)
                {
                    case "Date Created":
                        #region Date Created
                        {
                            DateTime dateTime = ShellAPI.FileTimeToDateTime(info.ctime);
                            string time = dateTime.ToLongTimeString();
                            retVal = string.Format("{0} {1}",
                                dateTime.ToShortDateString(),
                                time.Remove(time.Length - 3, 3));
                        }
                        #endregion
                        break;

                    case "Date Modified":
                        #region Date Modified
                        {
                            DateTime dateTime = ShellAPI.FileTimeToDateTime(info.mtime);
                            string time = dateTime.ToLongTimeString();
                            retVal = string.Format("{0} {1}",
                                dateTime.ToShortDateString(),
                                time.Remove(time.Length - 3, 3));
                        }
                        #endregion
                        break;
                }
            }

            return retVal;
        }

        public HorizontalAlignment GetAlignment(string columnName)
        {
            if (columnName == "Size")
                return HorizontalAlignment.Right;
            else
                return HorizontalAlignment.Left;
        }

        #endregion

        #region IBrowserPlugin Members

        public string Name
        {
            get { return "Default IColumnPlugin"; }
        }

        public string Info
        {
            get { return "Column plugin for the default columns for all items."; }
        }

        #endregion

        private string GetSizeString(long bytes)
        {
            if (bytes < 1000)
                return string.Format("{0} bytes", bytes);
            else if (bytes < 1000000)
                return string.Format("{0} KB", Math.Round((double)bytes / 1000, 3, MidpointRounding.ToEven));
            else if (bytes < 1000000000)
                return string.Format("{0} MB", Math.Round((double)bytes / 1000000, 3, MidpointRounding.ToEven));
            else
                return string.Format("{0} GB", Math.Round((double)bytes / 1000000000, 3, MidpointRounding.ToEven));
        }
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Netherlands Netherlands
I'm a student in Amsterdam (The Netherlands). I study Artificial Intelligence at the University of Amsterdam and I'm very fond of programming.

I discovered .Net programming a few years ago and immediately liked the Visual Studio environment. Since then I experimented a lot with .Net.

Comments and Discussions