Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes the contents of this configuration to a string in the format of an XmlDocument
        /// </summary>
        /// <returns></returns>
        public string ToXml()
        {
            MemoryStream           stream = new MemoryStream();
            XmlConfigurationWriter writer = new XmlConfigurationWriter();

            writer.Write(this, stream, true);
            string xml = System.Text.Encoding.ASCII.GetString(stream.GetBuffer());

            stream.Close();
            return(xml);
        }
        /// <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>
		/// 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>
		/// Writes the contents of this configuration to a string in the format of an XmlDocument
		/// </summary>
		/// <returns></returns>
		public string ToXml()
		{
			MemoryStream stream = new MemoryStream();
			XmlConfigurationWriter writer = new XmlConfigurationWriter();
			writer.Write(this, stream, true);			
			string xml = System.Text.Encoding.ASCII.GetString(stream.GetBuffer());
			stream.Close();
			return xml;
		}