Created
March 30, 2014 08:11
-
-
Save jstadler/9869331 to your computer and use it in GitHub Desktop.
Configuration file parser for configurations files that follow the format: [section]<EOL> parameter = value<EOL> ;comment<EOL>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
namespace FileReader | |
{ | |
class Program | |
{ | |
static string section = ""; | |
static string parameter = ""; | |
static string value = ""; | |
static void Main(string[] args) | |
{ | |
using ( StreamReader sr = new StreamReader("config.txt")) | |
{ | |
String line; | |
while ((line = sr.ReadLine()) != null) | |
{ | |
line = line.Trim(); // leading and trailing white space | |
if (line == "") continue; // skip empty lines | |
if (line[0] == ';') continue; // skip comments | |
line = Regex.Replace(line, @"\s+", ""); // remove spaces | |
if (line[0] == '[') // this indicates a section | |
{ | |
section = line.Substring(1, line.Length - 2); | |
Console.WriteLine("Section: " + section); | |
}else{ // found a parameter | |
int pos = line.IndexOf('='); | |
if (pos == -1) continue; // if no '=', skip line | |
parameter = line.Substring(0, pos); | |
value = line.Substring(pos + 1); | |
Console.WriteLine("\tParameter: " + parameter); | |
Console.WriteLine("\tValue: " + value); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment