sharp-reflective-configuration/FlatFileConfiguration.cs

50 lines
1.3 KiB
C#

using System;
using System.IO;
using System.Linq;
namespace ReflectiveConfiguration
{
public class FlatFileConfiguration : Configuration
{
public FlatFileConfiguration()
{
}
public override ConfigurationSection createSection(string name)
{
if (this.Keys.Contains(name))
throw new ArgumentException(String.Format("Section {0} already exists",name));
return new ConfigurationSection(this,name);
}
public override void LoadFrom(Stream stream)
{
StreamReader reader = new StreamReader(stream);
ConfigurationSection currentSection = this[""];
while (!reader.EndOfStream){
String line = reader.ReadLine().Trim();
if (line.StartsWith("[",StringComparison.InvariantCulture)){
currentSection = this[ line.Substring(1,line.Length-2)];
} else {
String[] tokens = line.Split(new char[] { '=' }, 2);
currentSection[tokens[0]] = tokens[1];
}
}
reader.Close();
}
public override void SaveTo(Stream stream)
{
throw new NotImplementedException();
}
}
}