Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Txt file as config file [closed]

I found application that use .txt files as config files.

The Files looks like

[[plugin.save]]=[[Save]]

how do I use it in C#?

how can I read The file to my application to use Values in [] as config?

like image 639
Asaf Shazar Avatar asked Dec 24 '22 07:12

Asaf Shazar


2 Answers

You have to read it as a regular file. Reading it use Dictionary to store values. Example code:

        Dictionary<string, string> configuration = new Dictionary<string, string>();

        Regex r = new Regex(@"\[\[(\w+)\]\]=\[\[(\w+)\]\]");

        string[] configArray = {"[[param1]]=[[Value1]]", "[[param2]]=[[Value2]]"};// File.ReadAllLines("some.txt");

        foreach (string config in configArray)
        {
             Match m = r.Match(config);
            configuration.Add(m.Groups[1].Value, m.Groups[2].Value);
        }

Please note to check for possible null values. Note also that regex expression should be different if config values can contain for example spaces.

like image 55
Marcin Iwanowski Avatar answered Dec 29 '22 06:12

Marcin Iwanowski


Just use:

var config = File.ReadAllLines(FileLocation)

And then parse it with the

String.Split()

Might use regex as well.

like image 32
Vladimir Avatar answered Dec 29 '22 06:12

Vladimir