/// <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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets an XmlConfiguration using a elementName, optionally creates a new or loads an existing configuration (.xml) file using the path specified, and optionally adds it to the collection if new
        /// </summary>
        /// <param name="elementName">The elementName by which this collection will be accessed</param>
        /// <param name="createIfNotFound">A flag indicating whether the configuration should be created if it does not exist</param>
        /// <param name="path">The path to the configuration</param>
        public XmlConfiguration this[string elementName, bool createIfNotFound, bool addToCollectionIfNew, string path]
        {
            get
            {
                /// try and find the configuration in the collection
                XmlConfiguration configuration = this[elementName];

                /// if it's not in the collection
                if (configuration == null)
                {
                    /// perhaps it does in the filesystem, if so then read it, and optionally add it to the collection
                    if (File.Exists(path))
                    {
                        try
                        {
                            FileStream             stream = new FileStream(path, FileMode.Open, FileAccess.Read);
                            XmlConfigurationReader reader = new XmlConfigurationReader();
                            configuration      = reader.Read(stream);
                            configuration.Path = path;
                            stream.Close();

                            if (addToCollectionIfNew)
                            {
                                this.Add(configuration);
                            }

                            return(configuration);
                        }
                        catch (System.Exception systemException)
                        {
                            System.Diagnostics.Trace.WriteLine(systemException);
                        }
                    }

                    /// apparently it doesnt' exist in the filesystem
                    if (createIfNotFound)
                    {
                        /// so create a new file
                        configuration             = new XmlConfiguration();
                        configuration.Path        = path;
                        configuration.ElementName = elementName;

                        /// save the blank config
                        Directory.CreateDirectory(path);
                        FileStream             stream = new FileStream(path, FileMode.Create, FileAccess.Write);
                        XmlConfigurationWriter writer = new XmlConfigurationWriter();
                        writer.Write(configuration, stream, false);
                        stream.Close();

                        /// add it to the config if so instructed
                        if (addToCollectionIfNew)
                        {
                            this.Add(configuration);
                        }
                    }
                }
                return(configuration);
            }
        }
		/// <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>
		/// Gets an XmlConfiguration using a elementName, optionally creates a new or loads an existing configuration (.xml) file using the path specified, and optionally adds it to the collection if new 
		/// </summary>
		/// <param name="elementName">The elementName by which this collection will be accessed</param>
		/// <param name="createIfNotFound">A flag indicating whether the configuration should be created if it does not exist</param>
		/// <param name="path">The path to the configuration</param>		
		public XmlConfiguration this[string elementName, bool createIfNotFound, bool addToCollectionIfNew, string path]
		{
			get
			{
				/// try and find the configuration in the collection
				XmlConfiguration configuration = this[elementName];
				
				/// if it's not in the collection
				if (configuration == null)
				{
					/// perhaps it does in the filesystem, if so then read it, and optionally add it to the collection
					if (File.Exists(path))
					{
						try
						{
							FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
							XmlConfigurationReader reader = new XmlConfigurationReader();
							configuration = reader.Read(stream);
							configuration.Path = path;								
							stream.Close();

							if (addToCollectionIfNew)
								this.Add(configuration);

							return configuration;
						} 
						catch (System.Exception systemException)
						{
							System.Diagnostics.Trace.WriteLine(systemException);
						}
					}

					/// apparently it doesnt' exist in the filesystem
					if (createIfNotFound)
					{
						/// so create a new file
						configuration = new XmlConfiguration();						
						configuration.Path = path;	
						configuration.ElementName = elementName;

						/// save the blank config
						Directory.CreateDirectory(path);
						FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
						XmlConfigurationWriter writer = new XmlConfigurationWriter();
						writer.Write(configuration, stream, false);
						stream.Close();						

						/// add it to the config if so instructed
						if (addToCollectionIfNew)
							this.Add(configuration);
					}
				}
				return configuration;
			}
		}