How To Create A Custom Configuration File Using C#


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



File:  MyConfiguration.cs


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

namespace DevelopersAlley.Configuration
    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())));

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




No notes on this topic.