Click here to Skip to main content
15,895,746 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.8K   2.3K   119  
A dual pane file manager for Windows XP.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using IWshRuntimeLibrary;
using Win32API;

namespace JGFSControls
{
	/// <summary>
	/// This class forms the Tag data for ToolStripMenuItem in the Crumb ToolBar, TreeNodes and ListViewItems
	/// </summary>
	public class JTagData
	{
		internal bool CanDropOnObject = false;
		internal bool ObjectIsEditable = false;

		internal DateTime LastWriteTime;

		internal System.IO.DriveType DriveType;

		internal Image ThumbImage;

		internal int ImageIndex;
		internal int SelectedImageIndex;
		internal int ThumbImageIndex;

		internal long AvailableFreeSpace = 0L;
		internal long Size = 0L;
		internal long TotalSize = 0L;

		internal ObjectTypes ObjectType = ObjectTypes.NONE;
		internal JShortcuts.ShortCutTypes LinkType = JShortcuts.ShortCutTypes.NONE;

		internal string DisplayName;
		internal string LinkPath;
		internal string Path;
		internal string SortName;
		internal string TypeName;

		internal uint Mask;
		internal uint StateMask;
		internal uint State;

		//	NB Only add file types after FILE as we use > ObjectType.FILE!
		internal enum ObjectTypes		
		{
			NONE,
			DESKTOP,
			COMPUTER,
			NETHOOD,
			DRIVE,
			FOLDER,
			NETSHARE,
			NETWORKCOMPUTER,
			DUMMY,
			FILE,
			LNK,
			SCF,
			URL
		}

		//	Create new from old
		internal JTagData(JTagData tagDataIN)
		{
			this.AvailableFreeSpace = tagDataIN.AvailableFreeSpace;
			this.CanDropOnObject = tagDataIN.CanDropOnObject;
			this.DisplayName = tagDataIN.DisplayName;
			this.DriveType = tagDataIN.DriveType;
			this.ImageIndex = tagDataIN.ImageIndex;
			this.LastWriteTime = tagDataIN.LastWriteTime;
			this.LinkPath = tagDataIN.LinkPath;
			this.LinkType = tagDataIN.LinkType;
			this.Mask = tagDataIN.Mask;
			this.ObjectIsEditable = tagDataIN.ObjectIsEditable;
			this.ObjectType = tagDataIN.ObjectType;
			this.Path = tagDataIN.Path;
			this.SelectedImageIndex = tagDataIN.SelectedImageIndex;
			this.Size = tagDataIN.Size;
			this.SortName = tagDataIN.SortName;
			this.State = tagDataIN.State;
			this.StateMask = tagDataIN.StateMask;
			this.ThumbImage = new Bitmap(tagDataIN.ThumbImage, tagDataIN.ThumbImage.Size);
			this.ThumbImageIndex = tagDataIN.ThumbImageIndex;
			this.TotalSize = tagDataIN.TotalSize;
			this.TypeName = tagDataIN.TypeName;
		}

		internal JTagData(ObjectTypes objectType)
		{
			if (objectType == ObjectTypes.COMPUTER)
				GetComputerInformation();
		}

		internal JTagData(DriveInfo drivInfo)
		{
			this.Path = drivInfo.RootDirectory.ToString();

			JCommon.GetTagDataInfo(this, true, true);
			JCommon.GetTVImageMaskFromPath(this, drivInfo.IsReady);

			this.ObjectType = JTagData.ObjectTypes.DRIVE;
			this.SortName = GetSortPrefix(this) + this.Path;
			this.DriveType = drivInfo.DriveType;
			bool driveIsWriteable = (drivInfo.DriveType == System.IO.DriveType.Fixed) || (drivInfo.DriveType == System.IO.DriveType.Removable) || (drivInfo.DriveType == System.IO.DriveType.Network);
			this.CanDropOnObject = (driveIsWriteable && drivInfo.IsReady);
			this.ObjectIsEditable = false;

			try
			{
				this.TotalSize = drivInfo.TotalSize;
				this.AvailableFreeSpace = drivInfo.AvailableFreeSpace;
			}
			catch
			{
			}
		}

		internal JTagData(DirectoryInfo dInfo)
		{
			this.Path = dInfo.FullName;

			JCommon.GetTagDataInfo(this, true, true);
			JCommon.GetTVImageMaskFromPath(this, true);

			this.ObjectType = JTagData.ObjectTypes.FOLDER;
			this.SortName = GetSortPrefix(this) + this.DisplayName;
			this.ObjectIsEditable = JCommon.IsObjectEditable(dInfo);
			this.CanDropOnObject = this.ObjectIsEditable;

			try
			{
				this.LastWriteTime = dInfo.LastWriteTime;
			}
			catch
			{
			}

			this.Size = 0L;
		}

		internal JTagData(FileInfo fInfo)
		{
			ObjectTypes objectType = ObjectTypes.NONE;

			//	Set Path- used by GetTagDataInfo
			this.Path = fInfo.FullName;

			JCommon.GetTagDataInfo(this, true, true);
			JCommon.GetTVImageMaskFromPath(this, true);

			try
			{
				this.LastWriteTime = fInfo.LastWriteTime;
				this.Size = fInfo.Length;
			}
			catch
			{
			}

			//	Gets ThumbImage
			SetThumbnail(fInfo);

			SetFileType(fInfo, ref objectType);
			this.ObjectType = objectType;
			this.SortName = GetSortPrefix(this) + this.DisplayName;
			this.ObjectIsEditable = true;
		}

		private void GetComputerInformation()
		{
			//	My Computer
			Image imgTemp = null;
			int imageIndex = 0, selectedImageIndex = 0;

			this.DisplayName = SystemInformation.ComputerName;
			this.Path = SystemInformation.ComputerName;
			this.ObjectType = ObjectTypes.COMPUTER;
			this.SortName = GetSortPrefix(this) + this.DisplayName;

			JFSFunctions.GetComputerInfo(true, out imgTemp, out imageIndex, out selectedImageIndex, out this.TypeName);

			this.ImageIndex = imageIndex;
			this.SelectedImageIndex = selectedImageIndex;

			this.ObjectIsEditable = false;

			this.ThumbImage = imgTemp;
		}

		private string GetSortPrefix(JTagData tagData)
		{
			string sortPrefix = "";

			switch (tagData.ObjectType)
			{
				case JTagData.ObjectTypes.NONE:
				case JTagData.ObjectTypes.DUMMY:
					sortPrefix = "";
					break;

				case JTagData.ObjectTypes.DESKTOP:
					sortPrefix = "0";
					break;

				case JTagData.ObjectTypes.COMPUTER:
					sortPrefix = "1";
					break;

				case JTagData.ObjectTypes.NETHOOD:
					sortPrefix = "2";
					break;

				case JTagData.ObjectTypes.DRIVE:
					sortPrefix = "3";
					break;

				case JTagData.ObjectTypes.FOLDER:
					sortPrefix = "4";
					break;

				case JTagData.ObjectTypes.NETSHARE:
					sortPrefix = "5";
					break;

				case JTagData.ObjectTypes.NETWORKCOMPUTER:
					sortPrefix = "6";
					break;

				default:
					sortPrefix = "7";
					break;
			}

			return sortPrefix;
		}

		private string ReadShortcut(FileInfo fInfo, out JShortcuts.ShortCutTypes iType)
		{
			iType = JShortcuts.ShortCutTypes.NONE;
			string targetPath = "";

			IWshShell_Class iwsShell = new IWshShell_Class();
			IWshShortcut_Class iwsShortcut;

			if (fInfo.Exists)
			{
				iwsShortcut = iwsShell.CreateShortcut(fInfo.FullName) as IWshShortcut_Class;
				targetPath = iwsShortcut.TargetPath;
				try
				{
					DirectoryInfo dInfo = new DirectoryInfo(targetPath);
					if (dInfo.Exists)
					{
						iType = JShortcuts.ShortCutTypes.FOLDER;
					}
					else
					{
						FileInfo fInfoCheck = new FileInfo(targetPath);
						if (fInfoCheck.Exists)
						{
							iType = JShortcuts.ShortCutTypes.FILE;
						}
					}
				}
				catch
				{
				}
			}
			return targetPath;
		}

		private void SetFileType(FileInfo fInfo, ref JTagData.ObjectTypes objectType)
		{
			//	This is needed for e.g. drag and drop, translating shortcuts etc.
			//	There may be others to add?

			switch (fInfo.Extension.ToUpper())
			{
				case ".LNK":
					objectType = JTagData.ObjectTypes.LNK;
					break;

				case ".SCF":
					objectType = JTagData.ObjectTypes.SCF;
					break;

				case ".URL":
					objectType = JTagData.ObjectTypes.URL;
					break;

				default:
					objectType = JTagData.ObjectTypes.FILE;
					break;
			}

			if (objectType == JTagData.ObjectTypes.LNK)
				this.LinkPath = ReadShortcut(fInfo, out this.LinkType);
		}

		public override string ToString()
		{
			return this.DisplayName;
		}

		internal void SetThumbnail(FileInfo fInfo)
		{
			string imageFilter = "*.jpg;*.png;*.gif;*.bmp;*.ico";
			Image imgTemp;

			//	Only try for image for image files
			if (imageFilter.Contains(fInfo.Extension.ToLower()))
			{
				try
				{
					//	Image.FromFile locks the image until it is disposed so would prevent moving/deleting the file
					imgTemp = Image.FromFile(fInfo.FullName);
					this.ThumbImage = new Bitmap(imgTemp, 16, 16);
					imgTemp.Dispose();
				}
				catch
				{
				}
			}
			else
			{
				try
				{
					imgTemp = JFSFunctions.ImageFromPath(fInfo.FullName);
					this.ThumbImage = new Bitmap(imgTemp, 16, 16);
					imgTemp.Dispose();
				}
				catch
				{
				}
			}
		}


	}

	class JTagDataSorter : IComparer<JTagData>
	{
		public int Compare(JTagData td1, JTagData td2)
		{
			return string.Compare(td1.SortName, td2.SortName);
		}
	}

}

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