/// <summary>
        /// Reads a configuration using the specified encryption engine from the specified path
        /// </summary>
        /// <param name="encryptionEngine">The encryption engine to use while reading the configuration, null if no decryption is desired</param>
        /// <param name="configuration">The configuration to be read into</param>
        /// <param name="path">The path to be read from</param>
        /// <returns></returns>
        public static bool ReadConfiguration(FileEncryptionEngine encryptionEngine, out XmlConfiguration configuration, string path)
        {
            Stream stream = null;

            ConfigurationEngine.ResetLastException();
            try
            {
                configuration = new XmlConfiguration();
                stream        = (encryptionEngine != null ? encryptionEngine.CreateDecryptorStream(path) : new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None));
                XmlConfigurationReader reader = new XmlConfigurationReader();
                configuration      = reader.Read(stream);
                configuration.Path = path;
                configuration.SetHasUnpersistedChanges(false);

                return(true);
            }
            catch (System.Exception systemException)
            {
                System.Diagnostics.Trace.WriteLine(systemException);
                _lastException = systemException;
            }
            finally
            {
                try
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
                catch { }
            }
            configuration = null;
            return(false);
        }
Exemple #2
0
        /// <summary>
        /// Clones this configuration
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            XmlConfiguration        clone   = null;
            XmlConfigurationElement element = (XmlConfigurationElement)base.Clone();

            if (element != null)
            {
                clone = new XmlConfiguration(element);
                clone.ResetBeforeEdit();
                clone.ResetChanged();
                clone.ResetAfterEdit();
                clone.Path = this.Path;
                clone.SetHasUnpersistedChanges(this.HasUnpersistedChanges());
                clone.Categories = (XmlConfigurationCategoryCollection)this.Categories.Clone();
            }
            return(clone);
        }
        /// <summary>
        /// Writes a configuration using the specified encryption engine to the specified path
        /// </summary>
        /// <param name="encryptionEngine">The encryption engine to use while writing the configuration, null if no encryption is desired</param>
        /// <param name="configuration">The confiruration to write</param>
        /// <param name="path">The path to write it to</param>
        /// <returns></returns>
        public static bool WriteConfiguration(FileEncryptionEngine encryptionEngine, XmlConfiguration configuration, string path)
        {
            Stream stream = null;

            ConfigurationEngine.ResetLastException();
            try
            {
                if (configuration != null)
                {
                    if (configuration.HasUnpersistedChanges())
                    {
                        configuration.AcceptChanges();
                        stream = (encryptionEngine != null ? encryptionEngine.CreateEncryptorStream(path) : new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None));
                        XmlConfigurationWriter writer = new XmlConfigurationWriter();
                        writer.Write(configuration, stream, false);
                        configuration.SetHasUnpersistedChanges(false);
                    }
                    return(true);
                }
            }
            catch (System.Exception systemException)
            {
                System.Diagnostics.Trace.WriteLine(systemException);
                _lastException = systemException;
            }
            finally
            {
                try
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
                catch { }
            }
            return(false);
        }
		/// <summary>
		/// Writes a configuration using the specified encryption engine to the specified path
		/// </summary>
		/// <param name="encryptionEngine">The encryption engine to use while writing the configuration, null if no encryption is desired</param>
		/// <param name="configuration">The confiruration to write</param>
		/// <param name="path">The path to write it to</param>
		/// <returns></returns>
		public static bool WriteConfiguration(FileEncryptionEngine encryptionEngine, XmlConfiguration configuration, string path)
		{	
			Stream stream = null;
			ConfigurationEngine.ResetLastException();
			try
			{
				if (configuration != null)
				{
					if (configuration.HasUnpersistedChanges())
					{
						configuration.AcceptChanges();						
						stream = (encryptionEngine != null ? encryptionEngine.CreateEncryptorStream(path) : new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None));
						XmlConfigurationWriter writer = new XmlConfigurationWriter();				
						writer.Write(configuration, stream, false);	
						configuration.SetHasUnpersistedChanges(false);
					}
					return true;			
				}
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
				_lastException = systemException;
			}
			finally
			{
				try 
				{
					if (stream != null) stream.Close();
				}
				catch { }
			}
			return false;
		}
		/// <summary>
		/// Reads a configuration using the specified encryption engine from the specified path
		/// </summary>
		/// <param name="encryptionEngine">The encryption engine to use while reading the configuration, null if no decryption is desired</param>
		/// <param name="configuration">The configuration to be read into</param>
		/// <param name="path">The path to be read from</param>
		/// <returns></returns>
		public static bool ReadConfiguration(FileEncryptionEngine encryptionEngine, out XmlConfiguration configuration, string path)
		{
			Stream stream = null;
			ConfigurationEngine.ResetLastException();
			try
			{
				configuration = new XmlConfiguration();		
				stream = (encryptionEngine != null ? encryptionEngine.CreateDecryptorStream(path) : new FileStream(path, FileMode.Open, FileAccess.Read , FileShare.None));
				XmlConfigurationReader reader = new XmlConfigurationReader();
				configuration = reader.Read(stream);		
				configuration.Path = path;
				configuration.SetHasUnpersistedChanges(false);

				return true;	
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
				_lastException = systemException;
			}
			finally
			{
				try 
				{
					if (stream != null) stream.Close();
				}
				catch { }
			}
			configuration = null;
			return false;
		}
		/// <summary>
		/// Clones this configuration
		/// </summary>
		/// <returns></returns>
		public override object Clone()
		{
			XmlConfiguration clone = null;
			XmlConfigurationElement element = (XmlConfigurationElement)base.Clone();
			if (element != null)
			{
				clone = new XmlConfiguration(element);
				clone.ResetBeforeEdit();
				clone.ResetChanged();
				clone.ResetAfterEdit();
				clone.Path = this.Path;
				clone.SetHasUnpersistedChanges(this.HasUnpersistedChanges());
				clone.Categories =  (XmlConfigurationCategoryCollection)this.Categories.Clone();
			}	
			return clone;
		}