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

Laptop Backup

Rate me:
Please Sign up or sign in to vote.
4.45/5 (31 votes)
19 Jun 200517 min read 118.4K   4.6K   103  
A file backup system using remoting.
using System;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Text;
using LaptopCommunications;
using SharpUtils;
using System.Collections;
using System.Data;

namespace Laptop_Backup
{
	
	/// <summary>
	/// Summary description for BackUpTab.
	/// </summary>
	public class BackUpTab : System.Windows.Forms.TabPage
	{

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox computerName;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ListView itemsListView;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		private System.Windows.Forms.ColumnHeader columnHeader6;
		private System.Windows.Forms.ColumnHeader columnHeader7;
		private System.Windows.Forms.ColumnHeader columnHeader8;
		private System.Windows.Forms.ColumnHeader columnHeader9;
		private System.Windows.Forms.Button addNewButton;
		private System.Windows.Forms.Button editButton;
		private System.Windows.Forms.Button deleteButton;
		private System.Windows.Forms.Label ipLabel;
		private System.Windows.Forms.TextBox ipText;
		private System.Windows.Forms.Button validateButton;

		/// <summary>
		/// A variable to check if for a successful validation
		/// </summary>
		private bool bValidation;

		/// <summary>
		/// use the computer number as an identifier
		/// </summary>
		private int nComputerNumber;

		/// <summary>
		/// get the validation status for this tab
		/// </summary>
		public bool Validation
		{
			get
			{
				return bValidation;
			}
		}

		/// variables declared for setup and save access
		private string strComputerName;
		private string strIPAddress;
		private string strPortNumber;

		/// <summary>
		/// get and set the computer name
		/// </summary>
		public string ComputerName
		{
			get
			{
				return strComputerName;
			}
			set
			{
				strComputerName = value;
				ComputerDisplayName = value;
			}
		}

		/// <summary>
		/// get and set the ip address
		/// </summary>
		public string IPAddress
		{
			get
			{
				return strIPAddress;
			}
			set
			{
				strIPAddress = value;
				ipText.Text = value;
			}
		}


		/// <summary>
		/// Log progess as u go
		/// </summary>
		private SimpleEventLog eventLog;

		/// <summary>
		/// control the amount of information written
		/// </summary>
		private DebugLevel debugLevel;


		/// <summary>
		/// The current number for this tab used as an idenitifer in the client
		/// </summary>
		public int ComputerNumber
		{
			set
			{
				nComputerNumber = value;
			}
		}

		/// <summary>
		/// Allow the startup form access to the list view when loading data
		/// </summary>
		public ListView List
		{
			get
			{
				return itemsListView;
			}
		}

		private CommunicationChannel communicationsChannel;
		private System.ComponentModel.IContainer components;

		/// <summary>
		/// create a validation class object to test for validation
		/// </summary>
		private Validate validate;

		/// <summary>
		/// Get and Set the Computer Display Name
		/// </summary>
		public string ComputerDisplayName
		{
			get
			{
				return computerName.Text;
			}
			set
			{
				this.computerName.Text = value;
			}
		}

		/// <summary>
		/// Build the tab page 
		/// </summary>
		public BackUpTab( int computerNumber )
		{

			this.label1 = new System.Windows.Forms.Label();
			this.computerName = new System.Windows.Forms.TextBox();

			this.label2 = new System.Windows.Forms.Label();
			this.itemsListView = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader9 = new System.Windows.Forms.ColumnHeader();
			this.addNewButton = new System.Windows.Forms.Button();
			this.editButton = new System.Windows.Forms.Button();
			this.deleteButton = new System.Windows.Forms.Button();
			this.ipLabel = new System.Windows.Forms.Label();
			this.ipText = new System.Windows.Forms.TextBox();
			this.validateButton = new System.Windows.Forms.Button();

			// 
			// set up the page
			// 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.validateButton,
																		  this.ipText,
																		  this.ipLabel,
																		  this.deleteButton,
																		  this.editButton,
																		  this.addNewButton,
																		  this.itemsListView,
																		  this.label2,
																		  this.computerName,
																		  this.label1});
			this.Location = new System.Drawing.Point(4, 22);
			this.Name = "computer";
			this.Size = new System.Drawing.Size(592, 310);
			this.TabIndex = 0;
			this.Text = "Computer";

			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(104, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Computer Name";
			// 
			// computerName
			// 
			this.computerName.Location = new System.Drawing.Point(120, 8);
			this.computerName.Name = "computerName";
			this.computerName.Size = new System.Drawing.Size(120, 20);
			this.computerName.TabIndex = 1;
			this.computerName.Text = "";
			this.computerName.TextChanged += new System.EventHandler(this.OnNameChanged);

			/// add the ip validation stuff
			this.ipLabel.Location = new System.Drawing.Point( 16, 35 );
			this.ipLabel.Name = "ipLabel";
			this.ipLabel.Size = new System.Drawing.Size( 104, 16 );
			this.ipLabel.TabIndex = 0;
			this.ipLabel.Text = "IP Address";

			this.ipText.Location = new System.Drawing.Point( 120, 35 );
			this.ipText.Name = "ipText";
			this.ipText.Size = new System.Drawing.Size( 120, 20 );
			this.ipText.TabIndex = 2;
			this.ipText.Text = "";
			
			this.validateButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.validateButton.Location = new System.Drawing.Point( 256, 35 );
			this.validateButton.Name = "validateButton";
			this.validateButton.Text = "Validate";
			this.validateButton.Click += new System.EventHandler( this.OnValidate );

			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(104, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Items Set Up";
			// 
			// itemsListView
			// 
			this.itemsListView.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);

			this.itemsListView.FullRowSelect = true;
			this.itemsListView.Location = new System.Drawing.Point(16, 80);
			this.itemsListView.MultiSelect = false;
			this.itemsListView.Name = "itemsListView";
			this.itemsListView.Size = new System.Drawing.Size(560, 224);
			this.itemsListView.TabIndex = 3;
			this.itemsListView.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "File/Folder To Copy";
			this.columnHeader1.Width = 118;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Remote Location ( From )";
			this.columnHeader2.Width = 140;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "Local Location ( To )";
			this.columnHeader3.Width = 154;
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "Time";
			this.columnHeader4.Width = 50;
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "Checked Mode";
			this.columnHeader5.Width = 90;
			///
			/// column 6
			/// 
			this.columnHeader6.Text = "Directory Copy";
			this.columnHeader6.Width = 90;
			///
			/// column 7
			/// 
			this.columnHeader7.Text = "Every Days";
			this.columnHeader7.Width = 75;
			///
			/// column 8
			/// 
			this.columnHeader8.Text = "On Start";
			this.columnHeader8.Width = 75;
			///
			/// column 9
			/// 
			this.columnHeader9.Text = "Try Til Done";
			this.columnHeader9.Width = 90;

			// 
			// addNewButton
			// 
			this.addNewButton.Enabled = false;
			this.addNewButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.addNewButton.Location = new System.Drawing.Point(256, 8);
			this.addNewButton.Name = "addNewButton";
			this.addNewButton.TabIndex = 4;
			this.addNewButton.Text = "Add New";
			this.addNewButton.Click += new System.EventHandler(this.OnAddNew);
			// 
			// editButton
			// 
			this.editButton.Enabled = false;
			this.editButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.editButton.Location = new System.Drawing.Point(336, 8);
			this.editButton.Name = "editButton";
			this.editButton.TabIndex = 5;
			this.editButton.Text = "Edit";
			this.editButton.Click += new System.EventHandler(this.OnEdit);

			// 
			// deleteButton
			// 
			this.deleteButton.Enabled = false;
			this.deleteButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.deleteButton.Location = new System.Drawing.Point(416, 8);
			this.deleteButton.Name = "deleteButton";
			this.deleteButton.TabIndex = 7;
			this.deleteButton.Text = "Delete";
			this.deleteButton.Click += new System.EventHandler(this.OnDelete);

			bValidation = false;

			nComputerNumber = computerNumber;

			debugLevel = new DebugLevel( DebugLevel.currentLevel );
			eventLog = new SimpleEventLog( "Laptop Backup", false );

			/// allow these to be used without validation ( on second thoughts don't )
			deleteButton.Enabled = false;
			editButton.Enabled = false;
			addNewButton.Enabled = false;
			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();  
				}
			}

			base.Dispose( disposing );
		}


		/// <summary>
		/// Add a new file or directory to backup
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnAddNew(object sender, System.EventArgs e)
		{
			AddNew dialog = new AddNew( IPAddress, ComputerDisplayName, false );

			/// 27 July 2004 cancel bugfix
			/// 

			if( dialog.ShowDialog( this ) != DialogResult.OK )
			{
				return;
			}

			QuickRegistry reg = new QuickRegistry();

			reg.OpenKey( "HKEY_LOCAL_MACHINE", "Software\\Laptop Backup", true );
			if( reg.Error == true )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "Unable to open the main registry key " + reg.ErrorMessage );
				}
			}

			reg.OpenKeyFromCurrentKey( ComputerName, true );
			if( reg.Error == true )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "Unable to open the registry key " + ComputerName + " " + reg.ErrorMessage  );
				}

				MessageBox.Show( this, "Unable to open the registry key " + ComputerName + "\n" + reg.ErrorMessage, "Laptop Backup" );
				return;
			}

			itemsListView.Clear();
			LoadList( strComputerName, reg.GetSubKeyNames() );

		}

		/// <summary>
		/// Edit the currently selected item in the list
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnEdit(object sender, System.EventArgs e)
		{

			ListView.SelectedListViewItemCollection col = itemsListView.SelectedItems;
			if( col.Count == 0 )
				return;

			IEnumerator colEnum = col.GetEnumerator();
			colEnum.MoveNext();

			/// get the list view item
			ListViewItem item = ( ListViewItem )colEnum.Current;

			string strKeyName = item.SubItems[ 0 ].Text; 
			string strRemote = item.SubItems[ 1 ].Text;
			string strLocation = item.SubItems[ 2 ].Text;
			string strTime = item.SubItems[ 3 ].Text;
			string strCheckedMode = item.SubItems[ 4 ].Text;
			string strDirectoryCopy = item.SubItems[ 5 ].Text;
			string strEveryDay = item.SubItems[ 6 ].Text;
			string strOnStart = item.SubItems[ 7 ].Text; 
			string strTryTilDone = item.SubItems[ 8 ].Text;

			EditForm edit = new EditForm( strComputerName, strIPAddress, strKeyName, strRemote, strLocation, strTime, strCheckedMode, strDirectoryCopy, strEveryDay, strOnStart, strTryTilDone ); 
			
			/// 27 July 2004 cancel bugfix
			/// 

			if( edit.ShowDialog( this ) != DialogResult.OK )
			{
				return;
			}
		
			if( edit.Updated == true )
			{
				QuickRegistry reg = new QuickRegistry();

				reg.OpenKey( "HKEY_LOCAL_MACHINE", "Software\\Laptop Backup", true );
				if( reg.Error == true )
				{
					if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
					{
						eventLog.WriteError( "Unable to open the main registry key " + reg.ErrorMessage );
					}
				}

				reg.OpenKeyFromCurrentKey( ComputerName, true );
				if( reg.Error == true )
				{
					if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
					{
						eventLog.WriteError( "Unable to open the registry key " + ComputerName + " " + reg.ErrorMessage  );
					}

					MessageBox.Show( this, "Unable to open the registry key " + ComputerName + "\n" + reg.ErrorMessage, "Laptop Backup" );
					return;
				}

				itemsListView.Clear();
				LoadList( strComputerName, reg.GetSubKeyNames() );
			}
		}

		/// <summary>
		/// Delete the currently selected list item.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnDelete(object sender, System.EventArgs e)
		{
			QuickRegistry reg = new QuickRegistry();

			reg.OpenKey( "HKEY_LOCAL_MACHINE", "Software\\Laptop Backup", true );
			if( reg.Error == true )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "Unable to open the main registry key " + reg.ErrorMessage );
				}

				MessageBox.Show( this, "Unable to open the main registry key\n " + reg.ErrorMessage, "Laptop Backup" );
				return;
			}

			reg.OpenKeyFromCurrentKey( ComputerName, true );
			if( reg.Error == true )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "Unable to open the registry key " + ComputerName + " " + reg.ErrorMessage  );
				}

				MessageBox.Show( this, "Unable to open the registry key " + ComputerName + "\n" + reg.ErrorMessage, "Laptop Backup" );
				return;
			}

			ListView.SelectedListViewItemCollection col = itemsListView.SelectedItems;
			if( col.Count == 0 )
				return;

			IEnumerator colEnum = col.GetEnumerator();
			colEnum.MoveNext();

			/// get the list view item
			ListViewItem item = ( ListViewItem )colEnum.Current;

			reg.DeleteKey( false, item.Text, true );
			if( reg.Error == true )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "Unable to delete the registry keys for " + ComputerName );
				}

				MessageBox.Show( this, "Unable to delete " + item.Text, "Laptop Backup" );
				return;
			}
			else
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.All ) == true )
				{
					eventLog.WriteInformation( "Deleted " + item.Text );
				}
			}

			itemsListView.Clear();
			LoadList( strComputerName, reg.GetSubKeyNames() );
		}

		/// <summary>
		/// Update the current name
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnNameChanged(object sender, System.EventArgs e)
		{
			Text = computerName.Text;
			ComputerDisplayName = Text;
			ComputerName = Text;
		}

		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
		}


		public void Validate()
		{
			validate = null;
			
			strIPAddress = this.ipText.Text;

			/// check for rubbish
			if( strIPAddress == null || strIPAddress == "" )
			{
				MessageBox.Show( "Unable to validate an empty ip address" );
				bValidation = false;
				return;
			}

			QuickRegistry reg = new QuickRegistry();

			reg.OpenKey( "HKEY_LOCAL_MACHINE", "Software\\Laptop Backup", true );
			if( reg.Error == true )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "Unable to open the main registry key " + reg.ErrorMessage );
				}

				MessageBox.Show( this, "Unable to open the main registry key\n " + reg.ErrorMessage, "Laptop Backup" );
				return;
			}

			/// make sure that the address that you are trying to connect to is the
			/// address in the text box. 
			string strTextIPAddress = ipText.Text;
			if( strTextIPAddress != strIPAddress )
			{
				strIPAddress = strTextIPAddress;
				reg.OpenKeyFromCurrentKey( this.ComputerName, true );
				if( reg.Error == true )
				{
					if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
					{
						eventLog.WriteError( "Unable to open the registry key " + ComputerName + " " + reg.ErrorMessage );
					}

					MessageBox.Show( "Error updating the registry key for " + ComputerName + " " + reg.ErrorMessage );
					return;
				}

				reg.SetValue( "IPAddress", strTextIPAddress );
				reg.Close( false );
				reg.RevertToPrevious();
			}


			strPortNumber = ( string )reg.GetValue( "PortNumber" );
			if( reg.Error == true || strPortNumber == null )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.WarningsAndErrors ) == true )
				{
					eventLog.WriteError( "Unable to get the number for Laptop Backup setting to default " );
				}

				strPortNumber = "8086";
			}


			StringBuilder buffer = new StringBuilder();
			buffer.Append( "tcp://" );
			buffer.Append( strIPAddress );
			buffer.Append( ":" );
			buffer.Append( strPortNumber );
			buffer.Append( "/LaptopBackup" );


			try
			{
				communicationsChannel = ( CommunicationChannel )Activator.GetObject( typeof( CommunicationChannel ), buffer.ToString() ); 
			}
			catch( RemotingTimeoutException rtoExp )
			{
				MessageBox.Show( this, "RemotingTimeout Exception caught " + rtoExp.Message, "Laptop Backup" );
				return;
			}
			catch( NullReferenceException nullExp )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "The url for the object is invalid " + nullExp.Message );
				}

				MessageBox.Show( this, "Unable to set up the communications due to :- " + nullExp.Message, "Laptop Backup" );
				return;

			}
			catch( RemotingException remExp )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "The object type is not defined properly, it needs to be derived for a remoting class " + remExp.Message );
				}

				MessageBox.Show( this, "Unable to set up the communications due to :- " + remExp.Message, "Laptop Backup" );
				return;

			}
				/// an attempt to catch the dialog exception caught message that shows up when the computer
				/// being validated is down this is due to the fact that similar code will run from the service
				/// and a dialog attempting to pop up would be somewhat inapropriate
			catch( Exception exp )
			{
				if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
				{
					eventLog.WriteError( "An generic exception caught " + exp.Message );
				}

				MessageBox.Show( this, "Unable to set up the communications due to :- " + exp.Message, "Laptop Backup" );
				return;
			}
			

			/// set the function to be called when a request is to be validated
			if( communicationsChannel != null )
			{
				try
				{
					validate = communicationsChannel.GetValidation( nComputerNumber );
				}
				catch( RemotingException rem )
				{
					if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
					{
						eventLog.WriteError( "Remoting Exception caught trying to get validation " + rem.Message );
					}
					
					MessageBox.Show( this, "Exception caught " + rem.Message, "Laptop Backup" );
					return;
				}
				catch( Exception exp )
				{
					if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
					{
						eventLog.WriteError( "Exception caught trying to get validation " + exp.Message );
					}


					MessageBox.Show( this, "Exception caught " + exp.Message, "Laptop Backup" );
					return;
				}

				if( validate == null )
					return;

				if( validate.IsValidated == false || validate.ComputerNumber != nComputerNumber )
				{

					if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
					{
						eventLog.WriteError( "Validation has gone tits up with no error message ??? " );
					}

					MessageBox.Show( this, "Unable to validate the server", "Laptop Backup" );
				}

				if( debugLevel.TestDebugLevel( DebugLevelSet.All ) == true )
				{
					eventLog.WriteInformation( "Validation for " + buffer.ToString() + " has been successful" );
				}

				addNewButton.Enabled = true;
				deleteButton.Enabled = true;
				editButton.Enabled = true;

				ComputerName = computerName.Text;
				IPAddress = ipText.Text;
			}

		}

		/// <summary>
		/// Validate the remote computer can be reached
		/// </summary>
		private void OnValidate(object sender, System.EventArgs e)
		{
			Validate();
		}

		/// <summary>
		/// Load the information for the list view from the registry
		/// </summary>
		/// <param name="strKey"></param>
		/// <param name="childSubKeys"></param>
		public void LoadList( string strKey, string[] childSubKeys )
		{

			itemsListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							this.columnHeader1,
																							this.columnHeader2,
																							this.columnHeader3,
																							this.columnHeader4,
																							this.columnHeader5,
																							this.columnHeader6,
																							this.columnHeader7,
																							this.columnHeader8,
																							this.columnHeader9 });

			StringBuilder buffer = new StringBuilder();
			StringBuilder strName = new StringBuilder();
			StringBuilder strFullPath = new StringBuilder();
			StringBuilder strCheckedCopy = new StringBuilder();
			StringBuilder strDirectoryCopy = new StringBuilder();
			StringBuilder strHours = new StringBuilder();
			StringBuilder strMinutes = new StringBuilder();
			StringBuilder strLocation = new StringBuilder();
			StringBuilder strDays = new StringBuilder();
			StringBuilder strOnStart = new StringBuilder();
			StringBuilder strTryTilDone = new StringBuilder();

			QuickRegistry childReg = new QuickRegistry();

			foreach( string strChildKey in childSubKeys )
			{
				buffer.Remove( 0, buffer.ToString().Length );
				buffer.Append( "Software\\Laptop Backup" + "\\" + strKey + "\\" + strChildKey );

				if( childReg.OpenKey( "HKEY_LOCAL_MACHINE", buffer.ToString(), true ) == false )
				{
					if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
					{
						eventLog.WriteError( "Unable to open registry key " + strKey + "\\" + strChildKey );
					}

					MessageBox.Show( this, "Error opening the child key " + strChildKey + " off " + strKey, "Laptop Backup" );
					continue;
				}
				else
				{
					if( debugLevel.TestDebugLevel( DebugLevelSet.All ) == true )
					{
						eventLog.WriteInformation( "Opened registry key " + strKey + "\\" + strChildKey );
					}

					try
					{
						strName.Remove( 0, strName.ToString().Length );
						strName.Append( childReg.GetValue( "Name" ).ToString() );
						strFullPath.Remove( 0, strFullPath.ToString().Length );
						strFullPath.Append( childReg.GetValue( "FullPath" ).ToString() );
						try /// allow null
						{
							strCheckedCopy.Remove( 0, strCheckedCopy.ToString().Length );
							strCheckedCopy.Append( childReg.GetValue( "CheckedCopy" ).ToString() );
						}
						catch( NullReferenceException nrefexp )
						{
							if( debugLevel.TestDebugLevel( DebugLevelSet.All ) == true )
							{
								eventLog.WriteInformation( "Checked copy for " + strChildKey + " is null " + nrefexp.Message );
							}
						}
						try /// allow null
						{
							strDirectoryCopy.Remove( 0, strDirectoryCopy.ToString().Length );
							strDirectoryCopy.Append( childReg.GetValue( "DirectoryCopy" ).ToString() );
						}
						catch( NullReferenceException nrefexp )
						{
							if( debugLevel.TestDebugLevel( DebugLevelSet.All ) == true )
							{
								eventLog.WriteInformation( "Directory Copy for " + strChildKey + " is null " + nrefexp.Message );
							}
						}
						try /// allow null wont be once finished but earlier code didn't have it
						{
							strTryTilDone.Remove( 0, strTryTilDone.ToString().Length );
							strTryTilDone.Append( childReg.GetValue( "TryTilDone" ).ToString() );
						}
						catch( NullReferenceException nrefExp )
						{
							if( debugLevel.TestDebugLevel( DebugLevelSet.All ) == true )
							{
								eventLog.WriteInformation( "Try Til Done for " + strChildKey + " is null " + nrefExp.Message );
							}
						}


						strHours.Remove( 0, strHours.ToString().Length );
						strHours.Append( childReg.GetValue( "Hours" ).ToString() );
						strMinutes.Remove( 0, strMinutes.ToString().Length );
						strMinutes.Append( childReg.GetValue( "Minutes" ).ToString() );
						strLocation.Remove( 0, strLocation.ToString().Length );
						strLocation.Append( childReg.GetValue( "Location" ).ToString() );
						strDays.Remove( 0, strDays.ToString().Length );
						strDays.Append( childReg.GetValue( "NumDays" ).ToString() );
						strOnStart.Remove( 0, strOnStart.ToString().Length );
						strOnStart.Append( childReg.GetValue( "CopyOnStart" ).ToString() );
					}
					catch( NullReferenceException nrefexp )
					{
						if( debugLevel.TestDebugLevel( DebugLevelSet.Errors ) == true )
						{
							eventLog.WriteError( "Error a required registry variable is set to false " + nrefexp.Message );
						}

						MessageBox.Show( this, "One of the registry keys is invalid " + nrefexp.Message, "Laptop Backup" );
						continue;
					}

					ListViewItem item = this.List.Items.Add( strName.ToString() );
					item.SubItems.Add( strFullPath.ToString() );
					/// remove the local host from the string
					strLocation.Remove( 0, "LocalHost\\".Length );
					item.SubItems.Add( strLocation.ToString() );
					item.SubItems.Add( strHours.ToString() + ":" + strMinutes.ToString() );
					item.SubItems.Add( strCheckedCopy.ToString() );
					item.SubItems.Add( strDirectoryCopy.ToString() );
					item.SubItems.Add( strDays.ToString() );
					item.SubItems.Add( strOnStart.ToString() );
					item.SubItems.Add( strTryTilDone.ToString() );
				}
			}

		}

	}
}

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

Comments and Discussions