Click here to Skip to main content
15,891,204 members
Articles / Programming Languages / C#

NUnit Starter

Rate me:
Please Sign up or sign in to vote.
4.92/5 (6 votes)
13 Oct 2009Zlib34 min read 42.5K   470   39  
How to create Visual Studio 2008 Add-in compatible with Visual Studio 2003
/*
 * Filename:    Configuration.cs
 * Product:     NUnit Starter
 * Solution:    NUnitStarter.sln
 * Project:     AddIn
 * Description: Class that reads and stores addin settings.
 * Copyright:   Julijan Šribar, 2009
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the author(s) be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */
using System;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;

namespace NUnitStarter
{
    /// <summary>
    ///   Reads and stores settings in XML configuration file.
    /// </summary>
    class Configuration
    {
        #region ConfigurationState enumeration
        /// <summary>
        ///   Enumeration used to indicate the state of configuration file read.
        /// </summary>
        public enum ConfigurationState
        {
            OK,
            FileNotFound,
            Corrupt
        }
        #endregion // ConfigurationState enumeration

        #region Constructor

        /// <summary>
        ///   Creates configuration reading configuration file. In case of
        ///   failure, default configuration is set.
        /// </summary>
        public Configuration()
        {
            m_settings = LoadSettings();
        }

        #endregion // Constructor

        #region Public methods

        /// <summary>
        ///   Saves settings to configuration file.
        /// </summary>
        public void SaveSettings()
        {
            using (FileStream fs = new FileStream(FullFilename, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                serializer.Serialize(fs, m_settings, new XmlSerializerNamespaces(new XmlQualifiedName[] { XmlQualifiedName.Empty } ));
                m_configurationState = ConfigurationState.OK;
            }
        }

        #endregion // Public methods

        #region Public properties

        /// <summary>
        ///   Gets and sets configuration settings.
        /// </summary>
        public Settings Settings
        {
            get { return m_settings; }
            set { m_settings = value; }
        }

        /// <summary>
        ///   Gets configuration state.
        /// </summary>
        public ConfigurationState State
        {
            get { return m_configurationState; }
        }

        #endregion // Public properties

        #region Private methods

        /// <summary>
        ///   Loads settings from configuration file. If reading fails, default
        ///   settings are applied and configuration state is set to <c>ConfigurationState.FileNotFound</c>
        ///   or <c>ConfigurationState.Corrupt</c>.
        /// </summary>
        /// <returns>
        ///   Settings read or default settings if reding fails.
        /// </returns>
        private Settings LoadSettings()
        {
            try
            {
                m_configurationState = ConfigurationState.OK;
                using (FileStream fs = new FileStream(FullFilename, FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    return (Settings)serializer.Deserialize(fs);
                }
            }
            catch (FileNotFoundException)
            {
                m_configurationState = ConfigurationState.FileNotFound;
            }
            catch (InvalidOperationException)
            {
                m_configurationState = ConfigurationState.Corrupt;
            }
            return new Settings();
        }

        /// <summary>
        ///   Gets full filename for the configuration file.
        /// </summary>
        private string FullFilename
        {
            get { 
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                return Path.Combine(path, Filename); 
            }
        }

        #endregion // Private methods

        #region Private fields

        private Settings m_settings;

        private ConfigurationState m_configurationState;

        public const string Filename = "Settings.xml";

        #endregion // Private fields
    }
}

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, along with any associated source code and files, is licensed under The zlib/libpng License


Written By
Software Developer (Senior)
Croatia Croatia
Graduated at the Faculty of Electrical Engineering and Computing, University of Zagreb (Croatia) and received M.Sc. degree in electronics. For several years he was research and lecturing assistant in the fields of solid state electronics and electronic circuits, published several scientific and professional papers, as well as a book "Physics of Semiconductor Devices - Solved Problems with Theory" (in Croatian).
During that work he gained interest in C++ programming language and have co-written "C++ Demystified" (in Croatian), 1st edition published in 1997, 2nd in 2001, 3rd in 2010, 4th in 2014.
After book publication, completely switched to software development, programming mostly in C++ and in C#.
In 2016 coauthored the book "Python for Curious" (in Croatian).

Comments and Discussions