How To Create A Custom Configuration File Using C#



TODO:

Have you ever wanted to have your own configuration file that you could easily turn into an object?

 

SOULTION:

File:  MyConfiguration.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DevelopersAlley.Configuration
{
    [Serializable()]
    public class MyConfiguration
    {
        public string Name { get; set; }
        public string LogoURL { get; set; }
    }
}

 

In your calling class: 

 

//open up and use our config file
MyConfiguration _Configuration = null;
string _ConfigFile = "C:\\DevelopersAlley.config";
string _Name = "";
string _LogoURL = "";

using (StreamReader oReader = File.OpenText(_ConfigFile))
{
      _Configuration = (MyConfiguration)(new XmlSerializer(_Configuration.GetType()).Deserialize(new StringReader(oReader.ReadToEnd())));
     oReader.Close();

     _Name = _Configuration.Name;
     _LogoURL = _Configuration.LogoURL;
}

 

 

NOTES:

No notes on this topic.