Click here to Skip to main content
15,885,546 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.3K   2.2K   119  
A dual pane file manager for Windows XP.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace JFileManager
{
	internal class cFileNoteData
	{
		private bool m_blnRecordExists = false;

		private int m_intRecNum = -1;
		private int m_intIconIndex = 0;

		private string m_strRecordFileName = "";
		private string m_strRecordFolderName = "";
		private string m_strRecordFullPath = "";
		private string m_strRecordNotes = "";

		//	New standard layout for DB Name, Connection string and Table Name
		private static string m_strDBName = "FileNotes.mdb";
		private static string m_strConnectionString = cCommon.ConnectionString(m_strDBName);
		private static string m_strTableName = "tblFileNotes";

		internal cFileNoteData()
		{
		}

		internal cFileNoteData(string strPath)
		{
			FileInfo fInfo = new FileInfo(strPath);
			if (fInfo.Exists)
				GetRecord(fInfo.Name);
		}

		public static bool FileNotesExists()
		{
			string strPath = Path.Combine(Application.StartupPath, m_strDBName);
			try
			{
				FileInfo fInfo = new FileInfo(strPath);
				return fInfo.Exists;
			}
			catch
			{
				return false;
			}
		}

		public bool DeleteRecord()
		{
			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();

			bool blnReturn = false;

			string strQuery = "SELECT * FROM " + m_strTableName + " WHERE intRecNum =";
			strQuery += this.m_intRecNum.ToString();

			adCON.Open(m_strConnectionString, "", "", 0);
			adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
			if (!adRS.EOF)
			{
				adRS.Delete(ADODB.AffectEnum.adAffectCurrent);
				blnReturn = true;
			}

			adRS.Close();
			adCON.Close();
			return blnReturn;
		}

		public int FileCheck(ref ListView lv)
		{
			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();

			cFileNoteData fileNoteData;

			FileInfo fInfo;

			int intNumWrong = 0;

			ListViewItem lvItem;

			string strQuery = "SELECT * FROM " + m_strTableName;

			adCON.Open(m_strConnectionString, "", "", 0);
			adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);

			lv.Items.Clear();

			while (!adRS.EOF)
			{
				fileNoteData = new cFileNoteData();
				fileNoteData.GetRecordDetails(adRS);

				fInfo = new FileInfo(fileNoteData.m_strRecordFullPath);
				if (!fInfo.Exists)
				{
					lvItem = new ListViewItem(fileNoteData.m_strRecordFileName);
					lvItem.SubItems.Add(fileNoteData.m_strRecordFolderName);
					lvItem.Tag = fileNoteData;
					lv.Items.Add(lvItem);
					intNumWrong++;
				}

				adRS.MoveNext();
			}
			adRS.Close();
			adCON.Close();

			return intNumWrong;
		}

		private string GetField(ADODB.Recordset adRS, string strField)
		{
			string strReturn = "";

			if (adRS.Fields[strField] != null)
				strReturn = Convert.ToString(adRS.Fields[strField].Value);

			return strReturn;
		}

		public string GetRecord(string strFileName)
		{
			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();

			string strQuery = "SELECT * FROM " + m_strTableName + " WHERE strFileName =";
			strQuery += "\"";
			strQuery += strFileName;
			strQuery += "\"";

			adCON.Open(m_strConnectionString, "", "", 0);
			adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
			if (!adRS.EOF)
			{
				GetRecordDetails(adRS);
			}
			else
			{
				this.m_intRecNum = -1;
				this.m_blnRecordExists = false;
			}

			adRS.Close();
			adCON.Close();
			return this.m_strRecordNotes;
		}

		public bool GetRecordDetails(ADODB.Recordset adRS)
		{
			bool blnOK = false;

			try
			{
				this.m_intRecNum = Convert.ToInt32(GetField(adRS, "intRecNum"));
				this.m_strRecordFullPath = GetField(adRS, "strFullPath");
				this.m_strRecordFolderName = GetField(adRS, "strFolderName");
				this.m_strRecordFileName = GetField(adRS, "strFileName");
				this.m_strRecordNotes = GetField(adRS, "strNotes");
				this.m_intIconIndex = cCommon.GetSmallIconIndex(this.m_strRecordFullPath);
				this.m_blnRecordExists = true;
			}
			catch
			{
				this.m_intRecNum = -1;
				this.m_blnRecordExists = false;
			}

			return blnOK;
		}

		public int Search(string strSearchString, bool blnFileName, bool blnNotes, ref ListView lv)
		{
			if (strSearchString == "")
				return ShowAll(ref lv);

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();

			cFileNoteData fileNoteData;

			int intCount = 0;
			int intNumRecs = 0;

			ListViewItem lvItem;

			string strQuery;

			if (blnFileName && blnNotes)
			{
				strQuery = "SELECT * FROM " + m_strTableName + " WHERE strFileName LIKE ";
				strQuery += "\"%";
				strQuery += strSearchString;
				strQuery += "%\"";
				strQuery += " OR strNotes LIKE ";
				strQuery += "\"%";
				strQuery += strSearchString;
				strQuery += "%\"";
			}
			else if (blnFileName)
			{
				strQuery = "SELECT * FROM " + m_strTableName + " WHERE strFileName LIKE ";
				strQuery += "\"%";
				strQuery += strSearchString;
				strQuery += "%\"";
			}
			else if (blnNotes)
			{
				strQuery = "SELECT * FROM " + m_strTableName + " WHERE strNotes LIKE ";
				strQuery += "\"%";
				strQuery += strSearchString;
				strQuery += "%\"";
			}
			else
			{
				return -1;
			}

			adCON.Open(m_strConnectionString, "", "", 0);
			adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
			intNumRecs = adRS.RecordCount;
			while (!adRS.EOF)
			{
				fileNoteData = new cFileNoteData();
				fileNoteData.GetRecordDetails(adRS);
				lvItem = new ListViewItem(fileNoteData.m_strRecordFileName, fileNoteData.m_intIconIndex);
				lvItem.SubItems.Add(fileNoteData.m_strRecordFolderName);
				if (!blnNotes)
					lvItem.SubItems.Add("");
				else
				{
					if (fileNoteData.m_strRecordNotes.ToUpper().IndexOf(strSearchString.ToUpper()) != -1)
						lvItem.SubItems.Add(fileNoteData.m_strRecordNotes);
				}
				lvItem.Tag = fileNoteData;
				lvItem.ToolTipText = "Double Click To Go To File";
				lv.Items.Add(lvItem);

				intCount++;
				adRS.MoveNext();
			}

			adRS.Close();
			adCON.Close();
			return intNumRecs;
		}

		private int ShowAll(ref ListView lv)
		{
			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();

			cFileNoteData fileNoteData;

			int intCount = 0;
			int intNumRecs = 0;

			ListViewItem lvItem;

			string strQuery = "SELECT * FROM " + m_strTableName + " ORDER BY strFileName";

			adCON.Open(m_strConnectionString, "", "", 0);
			adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
			intNumRecs = adRS.RecordCount;
			while (!adRS.EOF)
			{
				fileNoteData = new cFileNoteData();
				fileNoteData.GetRecordDetails(adRS);
				lvItem = new ListViewItem(fileNoteData.m_strRecordFileName, fileNoteData.m_intIconIndex);
				lvItem.SubItems.Add(fileNoteData.m_strRecordFolderName);
				lvItem.SubItems.Add(fileNoteData.m_strRecordNotes);
				lvItem.Tag = fileNoteData;
				lvItem.ToolTipText = "Double Click To Go To File";
				lv.Items.Add(lvItem);

				intCount++;
				adRS.MoveNext();
			}

			adRS.Close();
			adCON.Close();
			return intNumRecs;
		}

		public bool UpdateRecord()
		{
			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();

			bool blnOK;

			object[] fList = { "strFullPath", "strFolderName", "strFileName", "strNotes" };

			object[] fValues = { this.m_strRecordFullPath, this.m_strRecordFolderName, this.m_strRecordFileName, this.m_strRecordNotes };

			string strQuery = "SELECT * FROM " + m_strTableName + " WHERE intRecNum =";
			strQuery += this.m_intRecNum.ToString();

			adCON.Open(m_strConnectionString, "", "", 0);
			adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);

			try
			{
				if (!adRS.EOF)
				{
					adRS.Update(fList, fValues);
					blnOK = true;
				}
				else
				{
					adRS.AddNew(fList, fValues);
					blnOK = true;
				}
			}
			catch
			{
				blnOK = false;
			}
			finally
			{
				adRS.Close();
				adCON.Close();
			}

			return blnOK;
		}

		//	***************************************************************	Public Properties

		public string FileName
		{
			get
			{
				return m_strRecordFileName;
			}
			set
			{
				m_strRecordFileName = value;
			}
		}

		public string FolderName
		{
			get
			{
				return m_strRecordFolderName;
			}
			set
			{
				m_strRecordFolderName = value;
			}
		}

		public string FullPath
		{
			get
			{
				return m_strRecordFullPath;
			}
			set
			{
				m_strRecordFullPath = value;
			}
		}

		public string Notes
		{
			get
			{
				return m_strRecordNotes;
			}
			set
			{
				m_strRecordNotes = value;
			}
		}

		public bool RecordExists
		{
			get
			{
				return m_blnRecordExists;
			}
		}

		public int RecordNumber
		{
			get
			{
				return this.m_intRecNum;
			}
		}


	}
}

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