LCKB
c# Loading mysql config - Printable Version

+- LCKB (https://lckb.dev/forum)
+-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109)
+--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196)
+---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192)
+---- Thread: c# Loading mysql config (/showthread.php?tid=2056)



- Jaiz - 06-16-2013


I coded a test tool mysql config tool. I can save my config but not load? Somebody can help me? 

try
{
System.IO.TextReader textreader1 = new System.IO.StreamReader("Jaiz.set");


textreader1.Close();
this.statuslbl.ForeColor = Color.Green;
statuslbl.Text = "Reading MySQL Config file Succesfull!";

}
catch
{

this.statuslbl.ForeColor = Color.Red;
statuslbl.Text = "Reading MySQL Config file failed!";
}

what i have to write next? 

maybe that? 

textreader1.ReadLine(textBox_host.Text);

but seem to not work. I hope somebody can help me




- Phantom - 06-16-2013


if you want to use config files like .ini format

[Area]
Entry=Data
Entry2=Data2
[Area2]
Entry=Data
Entry2=Data

this C# class will be helpful. the documentation is in german but it is still a realy helpful class

2

 

Greets




- someone - 06-29-2013


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"];




- xNiceEinz. - 02-25-2014



 

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




- xMeowx3 - 02-25-2014


Congratulations on necroing a thread from 8 months since last reply, if you want help with something create your own topic.

Closed.




- Johnny5 - 02-25-2014

A moderator closed this post. Reason: necrod.