Click here to Skip to main content
15,881,600 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.3K   4.6K   103  
A file backup system using remoting.
using System;
using System.IO;
using System.Security;

namespace SharpUtils
{
	/// <summary>
	/// Summary description for FileDetails.
	/// </summary>
	[Serializable]
	public class FileDetails : IDisposable
	{

		private string strLocation;
		private string strError;
		private bool bError;
		private FileStream readStream;
		private FileInfo fileInfo;
		private bool bNewFile;

		/// <summary>
		/// check if there is an error
		/// </summary>
		public bool Error
		{
			get
			{
				return bError;
			}
		}

		/// <summary>
		/// get the error message
		/// </summary>
		public string ErrorMessage
		{
			get
			{
				return strError;
			}
		}

		/// <summary>
		/// get the file info
		/// </summary>
		public FileInfo GetInfo
		{
			get
			{
				return fileInfo;
			}
		}

		/// <summary>
		/// get the file stream
		/// </summary>
		public FileStream GetStream
		{
			get
			{
				return readStream;
			}
		}

		/// <summary>
		/// check if the file is newly created
		/// </summary>
		public bool NewFile
		{
			get
			{
				return bNewFile;
			}
		}

		/// <summary>
		/// Need to be able to get around csharp thinking variable is not initialised when 
		/// calling new in a try blocak and attempting to use it outside of the catch statement
		/// </summary>
		public FileDetails()
		{
		}

		/// <summary>
		/// constructor
		/// </summary>
		/// <param name="strLocation">file to open</param>
		public FileDetails( string strLocation )
		{
			Open( strLocation );
		}


		public void Dispose()
		{
			readStream.Close();
		}


		/// <summary>
		/// open the file
		/// </summary>
		/// <param name="strLocation">file to open</param>
		/// <returns>true on success</returns>
		public bool Open( string strLocation )
		{
			bError = false;
			bNewFile = false;
			//
			// TODO: Add constructor logic here
			//

			this.strLocation = strLocation;

			try
			{
				fileInfo = new FileInfo( strLocation );
				if( fileInfo.Exists == false )
					bNewFile = true;
				readStream = fileInfo.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite );
			}
			catch( ArgumentNullException argNullExp )
			{
				strError = "Error argument null exception thrown getting the file " + strLocation + " " + argNullExp.Message;
				bError = true;
			}
			catch( SecurityException secExp )
			{
				strError = "Error security exception thrown getting the file " + strLocation + " " + secExp.Message;
				bError = true;
			}
			catch( ArgumentException argExp )
			{
				strError = "Error argument exception thrown getting the file " + strLocation + " " + argExp.Message;
				bError = true;
			}
			catch( UnauthorizedAccessException unaExp )
			{
				strError = "Error unauthorised access exception thrown getting the file " + strLocation + " " + unaExp.Message;
				bError = true;
			}
			catch( PathTooLongException ptolExp )
			{
				strError = "Error path too long exception thrown getting the file " + strLocation + " " + ptolExp.Message;
				bError = true;
			}
			catch( NotSupportedException nsepExp )
			{
				strError = "Error security exception thrown getting the file " + strLocation + " " + nsepExp.Message;
				bError = true;
			}
			catch( IOException ioExp )
			{
				strError = "Error io exception thrown getting the file " + strLocation + " " + ioExp.Message;
				bError = true;
			}

			return bError == true? false:true;
		}


	}
}

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