Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

Dual Pane File Manager

Rate me:
Please Sign up or sign in to vote.
4.64/5 (28 votes)
21 Dec 2010CPOL8 min read 140.7K   2.3K   119  
A dual pane file manager for Windows XP.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace JFileManager
{
	/// <summary>
	/// Privides additional information to the standard ListViewItem
	/// </summary>
	public class cLVItem : ListViewItem
	{
		public ObjectType ObjectType = ObjectType.NONE;
		public string Path = "";
		public string TypeName = "";
		public DriveType DriveType = DriveType.Unknown;		
		public ObjectType LinkType = ObjectType.NONE;
		public string LinkPath = "";
		public long Size = 0L;
		public long TotalSize = 0L;
		public long AvailableFreeSpace = 0L;
		public DateTime LastWriteTime;
		public FileAttributes Attributes;
		public string SortName = "";
		public bool IsReady = false;
		public int Mask = 0;
		public int StateMask = 0;
		public int State = 0;
		public string VolumeLabel = "";

		public cLVItem(cJDriveInfo jDriveInfo)
		{
			//	Class Data
			this.ObjectType = jDriveInfo.ObjectType;
			this.Path = jDriveInfo.Path;
			this.TypeName = jDriveInfo.TypeName;
			this.TotalSize = jDriveInfo.TotalSize;
			this.AvailableFreeSpace = jDriveInfo.AvailableFreeSpace;
			this.DriveType = jDriveInfo.DriveType;
			this.IsReady = jDriveInfo.IsReady;
			this.Mask = jDriveInfo.Mask;
			this.StateMask = jDriveInfo.StateMask;
			this.State = jDriveInfo.State;
			this.VolumeLabel = jDriveInfo.VolumeLabel;

			//	ListViewItem Data
			this.Text = jDriveInfo.DisplayName;
			this.ImageIndex = jDriveInfo.IconIndex;
		}

		public cLVItem(DirectoryInfo dInfo)
		{
			this.ObjectType = ObjectType.FOLDER;
			this.Path = dInfo.FullName;
			this.SortName = "0" + this.Path;
			
			//	There can be an issue here with some CD-R and virtual CD
			try
			{
				this.LastWriteTime = dInfo.LastWriteTime;
			}
			catch
			{
			}

			this.Attributes = dInfo.Attributes;
			this.Size = 0L;
			this.IsReady = true;

			this.GetSHFileInfo();

			cCommon.GetLVImageMask(this.Path, out this.Mask, out this.StateMask, out this.State);
		}

		public cLVItem(FileInfo fInfo)
		{
			ObjectType oType = ObjectType.NONE;

			this.Path = fInfo.FullName;
			this.SortName = "1" + this.Path;
			
			//	There can be an issue here with some CD-R and virtual CD
			try
			{
				this.LastWriteTime = fInfo.LastWriteTime;
				this.Size = fInfo.Length;
			}
			catch
			{
			}

			this.Attributes = fInfo.Attributes;

			this.GetSHFileInfo();

			SetFileType(fInfo, ref this.ObjectType);

			this.LinkPath = "";

			if (this.ObjectType == ObjectType.LNK)
			{
				this.LinkPath = cCommon.ReadShortcut(fInfo, out oType);
				if (oType == ObjectType.FOLDER)
				{
					this.LinkType = ObjectType.FOLDER;
				}
				if (oType == ObjectType.FILE)
				{
					this.LinkType = ObjectType.FILE;
				}
			}

			cCommon.GetLVImageMask(this.Path, out this.Mask, out this.StateMask, out this.State);
		}

		private void GetSHFileInfo()
		{
			SHFILEINFO shfi = new SHFILEINFO();

			UInt32 uFlags = (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_SMALLICON | SHGFI.SHGFI_TYPENAME | SHGFI.SHGFI_ADDOVERLAYS | SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_DISPLAYNAME);
			IntPtr ipTemp = cCommon.SHGetFileInfo(this.Path, 0, out shfi, Marshal.SizeOf(shfi), uFlags);
			
			// Check result
			if (ipTemp != IntPtr.Zero)
			{
				this.Text = shfi.szDisplayName;
				this.TypeName = shfi.szTypeName;
				this.ImageIndex = shfi.iIcon;
			}

			if (shfi.hIcon != IntPtr.Zero)
				cCommon.DestroyIcon(shfi.hIcon);
		}

		private void SetFileType(FileInfo fiCurrent, ref ObjectType oType)
		{
			//	This is needed for e.g. drag and drop, translating shortcuts etc.
			//	There may be other extensions to add?

			switch (fiCurrent.Extension.ToUpper())
			{
				case ".LNK":
					oType = ObjectType.LNK;
					break;

				case ".SCF":
					oType = ObjectType.SCF;
					break;

				case ".URL":
					oType = ObjectType.URL;
					break;

				default:
					oType = ObjectType.FILE;
					break;
			}
		}

		public bool CanWrite
		{
			get
			{
				return cUserFileAccessRights.CanWrite(this.Path);
			}
		}

		public bool IsEditable
		{
			get
			{
				if (this.ObjectType < ObjectType.FILE && this.ObjectType != ObjectType.FOLDER)
					return false;
				return this.CanWrite;
			}
		}

		public bool IsWriteable
		{
			get
			{
				return (this.DriveType == DriveType.Fixed) || (this.DriveType == DriveType.Removable) || (this.DriveType == DriveType.Network);
			}
		}

		public void UpdateDriveData(cJDriveInfo jDriveInfo)
		{
			cJDriveInfo jDriveInfoNew = cJDriveInfoCollection.UpdateDriveData(jDriveInfo);
			//	Class Data
			this.ObjectType = jDriveInfoNew.ObjectType;
			this.Path = jDriveInfoNew.Path;
			this.TypeName = jDriveInfoNew.TypeName;
			this.TotalSize = jDriveInfoNew.TotalSize;
			this.AvailableFreeSpace = jDriveInfoNew.AvailableFreeSpace;
			this.DriveType = jDriveInfoNew.DriveType;
			this.IsReady = jDriveInfoNew.IsReady;
			this.Mask = jDriveInfoNew.Mask;
			this.StateMask = jDriveInfoNew.StateMask;
			this.State = jDriveInfoNew.State;
			this.VolumeLabel = jDriveInfoNew.VolumeLabel;

			//	ListViewItem Data
			this.Text = jDriveInfoNew.DisplayName;
			this.ImageIndex = jDriveInfoNew.IconIndex;
		}

	}
}

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
Retired
United Kingdom United Kingdom
I have been a keen hobbyist programmer since getting my first computer - a Vic 20 (you had to be able to write programs then since few programs were available and all were expensive).
Retired and now living in Pewsey, Wiltshire, where I spend (far too much of) my time writing computer programs to keep my mind active.

Comments and Discussions