How to (re)use the .ini files

G

G.Esmeijer

Friends,

Coming from vb6 I used .ini files to put in somem configuration issues for
my application.
Although the structure might be know to you, here is an example


[General]
ShowNetprices=1
useBarcod=0

I used to read these with some Api related read and write profilestring
function
How can I read these kind of files fromout an C# program.
or do I really have to put these configuration aspects in a XML file

Who can help me or even better who has some code snippits for doing so

Regards
gerrit Esmeijer
 
D

Doug Erickson [MS]

There's no reason why you can't read them in as a flat text file, although
using the registry for settings like this is almost always preferable.

That said, you can use the StreamReader class in System.IO to read in these
values. For example (note that there may be a few syntax errors below -- I
typed this off the top of my head -- but that the concept is correct):

using System.IO;

....
string iniStr = "";
int iniShowNetprices = 0;
int iniUseBarcod = 0;

if (File.Exists(pathToIniFile))
{
StreamReader sr = new StreamReader(PathToIniFile);
while (sr.Peek >= 0)
{
iniStr = sr.ReadLine();
if (iniStr.IndexOf("ShowNetprices") >= 0)
iniShowNetprices =
Convert.ToInt32(initStr.Split('=')[1].Replace("\r\n", ""));
if (iniStr.IndexOf("useBarcod") >= 0)
iniUseBarcod =
Convert.ToInt32(initStr.Split('=')[1].Replace("\r\n", ""));
}

sr.Close();
sr = null;
}


--
 
G

G.Esmeijer

Thanks for the response,

I will try both so find out which one suits me best

Gerrit. Esmeijer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top