02-25-2014, 06:40 PM
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"];
If i had added all as Class like this:
2
And call it on normal Form1.cs
2
How i get
now x or y for example?
Config file is this:
"
#ignore
x=1
y=2
"
Because if i use "string myValue = config["x"];"
It says me it cannot found "config"?
And if i want to use it on another class how i set now "x" where is SETXHERE on Screen below?
2

