Home All Groups Group Topic Archive Search About

System.Configuration reading and writing custom section

Author
19 May 2006 1:59 PM
Martin Venter
Hi Everyone,

I really hope someone can help me.
I've been using the micrsoft enterprise library since version 1 and I'm now
in the process of moving my project to VS 2005 and the latest enterprise
library.

In my project I use the configuration block from enterprise library.
So now I want to move to the configuration class that is in .NET 2.0

I don't know how to read and write my config data with the config structure
that I'm using.

Below is my config structure and class information that works whem I'm using
the configuration block of enterprise library.
What do I need to change to be able to do this with the configuration class
in .NET 2.0.

Currently the configuration data is in a seperate file but I dont care if it
needs to be in the app.config file. Either way will work for me.

I would really appreciate the help.

Here is my galactrixConfiguration.config file:

<?xml version="1.0" encoding="utf-8"?>
<galactrixConfiguration>
  <xmlSerializerSection type="Unison.Galactrix.Configuration.Settings,
Unison.Galactrix.Configuration, Version=1.5.0.3, Culture=neutral,
PublicKeyToken=null">
    <Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Reporter>
        <SystemId>0</SystemId>
        <ContentType />
        <Debug>true</Debug>
        <Schedule>60</Schedule>
        <ReportsPath>D:\Projects\Galactrix\GalactrixSln\Reporter\bin\Debug\temp\</ReportsPath>
      </Reporter>
      <Global>
        <MsdeCap>64</MsdeCap>
        <License>D:\Projects\Galactrix\GalactrixSln\gx_with_sms_with_hum.lic</License>
        <ReportsPath>D:\Source\D-Code\Galactrix\v100\Reports\</ReportsPath>
      </Global>
      <Mail>
        <SMTPAddress></SMTPAddress>
        <SMTPPassword></SMTPPassword>
        <SMTPPort>25</SMTPPort>
        <SMTPSender>Galactrix System</SMTPSender>
        <SMTPServer></SMTPServer>
        <SMTPUser></SMTPUser>
      </Mail>
      <File>
        <FaxServerAccountLog>C:\Galactrix\UniFax\Log\</FaxServerAccountLog>
        <Messages>\\servkomone\msgs\</Messages>
        <SysMsg>c:\Galactrix\SYSMSG.raw</SysMsg>
      </File>
      <UmsLine>
        <ConfigId>0</ConfigId>
      </UmsLine>
      <Web>
        <CommandTimeout>240</CommandTimeout>
        <TempDirectory>TempDir</TempDirectory>
      </Web>
      <Logger>
        <RecordDirectory>C:\Galactrix\rec\</RecordDirectory>
        <PlayDirectory>C:\Galactrix\play\</PlayDirectory>
        <ExtractDirectory>C:\Galactrix\extract\</ExtractDirectory>
      </Logger>
    </Settings>
  </xmlSerializerSection>
</galactrixConfiguration>

This is what my main class looks like:

#region ... Using ...

using System;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Windows.Forms;

using Nini.Config;

#endregion

namespace Unison.Galactrix.Configuration
{
  /// <summary>
  /// Summary description for GalactrixSettings.
  /// </summary>
  [Serializable]
  public class Settings
  {
    public const string SectionName = "galactrixConfiguration";
    //public const string ConfigurationNamespace =
"http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data";

    #region ... Private Properties ...

    private GlobalSettings global;
    private ReporterSettings reporter;
    private MailSettings mail;
    private FileSettings file;
    private WebSettings web;
    private LoggerSettings logger;
    private UmsLineSettings umsLine;

    #endregion

    #region ... Constructor ...

    public Settings()
    {
      reporter = new ReporterSettings();
      mail = new MailSettings();
      global = new GlobalSettings();
      file = new FileSettings();
      web = new WebSettings();
      logger = new LoggerSettings();
      umsLine = new UmsLineSettings();
    }

    #endregion

    #region ... Public Properties ...

    [Category("General")]
    public ReporterSettings Reporter
    {
      get { return reporter; }
      set { reporter = value; }
    }

    [Category("General")]
    public GlobalSettings Global
    {
      get { return global; }
      set { global = value; }
    }

    [Category("General")]
    public MailSettings Mail
    {
      get { return mail; }
      set { mail = value; }
    }

    [Category("General")]
    public FileSettings File
    {
      get { return file; }
      set { file = value; }
    }

    [Category("General")]
    public UmsLineSettings UmsLine
    {
      get { return umsLine; }
      set { umsLine = value; }
    }

    [Category("General")]
    public WebSettings Web
    {
      get { return web; }
      set { web = value; }
    }

    [Category("General")]
    public LoggerSettings Logger
    {
      get { return logger; }
      set { logger = value; }
    }

    #endregion
  }
}

For each of these properties in my Settings class I have a class for that
property like:

#region ... Using ...

using System;
using System.ComponentModel;

#endregion

namespace Unison.Galactrix.Configuration
{
  /// <summary>
  /// Summary description for MailSettings.
  /// </summary>
  [Serializable, TypeConverter(typeof(ExpandableObjectConverter))]
  public class MailSettings
  {
    #region ... Constructor ...

    public MailSettings()
    {
      _sMTPPort = 25;
    }

    #endregion

    #region ... Private Properties ...

    private string _sMTPServer;
    private int _sMTPPort;
    private string _sMTPSender;
    private string _sMTPUser;
    private string _sMTPPassword;
    private string _sMTPAddress;

    #endregion

    #region ... Public Properties ...

    public string SMTPAddress
    {
      get { return _sMTPAddress; }
      set { _sMTPAddress = value; }
    }

    public string SMTPPassword
    {
      get { return _sMTPPassword; }
      set { _sMTPPassword = value; }
    }

    public int SMTPPort
    {
      get { return _sMTPPort; }
      set { _sMTPPort = value; }
    }

    public string SMTPSender
    {
      get { return _sMTPSender; }
      set { _sMTPSender = value; }
    }

    public string SMTPServer
    {
      get { return _sMTPServer; }
      set { _sMTPServer = value; }
    }

    public string SMTPUser
    {
      get { return _sMTPUser; }
      set { _sMTPUser = value; }
    }

    #endregion
  }
}

Regards,
Martin Venter

AddThis Social Bookmark Button