c# Loading mysql config
#3

If you want to read/write ini files windows comes with build in functions (in what Phantom wrote)

WritePrivateProfileString
GetPrivateProfileString
For .net this link will help implement those functions in a .net application

2

2

 

If you want a simple config file, its simple to implement in few steps:

1) First read the file line by line:

2) Ignore lines that start with #

3) Split the lines  by = into key and value

4) Trim the key by all spaces ( optional for value)

5) Store the value in a dictionary(or a list, but a dictionary has a lot options)

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

private void Load(string filename)
{
//1) First read the file line by line:
System.IO.StreamReader sr = new System.IO.StreamReader(filename);
string line= "";
while ((line = sr.ReadLine()) != null)
{
//2) Ignore lines that start with #
if (line[0] == #) continue;

//3) Split the lines by = into key and value
string[] blocks = line.Split(=);
if (blocks.Length > 1)
{
//4) Trim the key by all spaces ( optional for value)
blocks[0] = blocks[0].Trim();
//5) Store the value in a dictionary(or a list, but a dictionary has a lot options)
config.Add(blocks[0], blocks[1]);
}
}
sr.Close();
}

Config file is this:

#ignore
x=1
y=2
Using it would be:

Load("File.cfg")
string myValue = config["x"];



Messages In This Thread
[No subject] - by Jaiz - 06-16-2013, 09:55 AM
[No subject] - by Phantom - 06-16-2013, 07:01 PM
[No subject] - by someone - 06-29-2013, 05:56 PM
[No subject] - by xNiceEinz. - 02-25-2014, 06:40 PM
[No subject] - by xMeowx3 - 02-25-2014, 07:47 PM
[No subject] - by Johnny5 - 02-25-2014, 07:48 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)