Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read settings from a ini file 1

Status
Not open for further replies.

Antithott

IS-IT--Management
Jan 20, 2006
90
DK
Hi,

Possibly this question is very simple, but im looking for the best way to read settings from a ini file. I can do it by put each setting on a new line, but i want to read a ini file which contains sections, Example :

[smtp]
Server = User = test
Password = test

[database]
address = C:\database.mdb

How would i search for the specific section of the ini file, and then read the lines only for that section?

- Antithott
 
Well I'm not sure if what I've got for you covers your exact question (read only lines from section), but I found an IniReader class quite some time ago that worked really nice for reading/writing ini files.


However I would recommend XML to store program settings as a better file format.
 
I use an XML file with a key-value pair. That way I can use the dataset.readxml and dataset.writexml to open and save the settings. And with a two column table (key and value) you can easily select the value that you are looking for.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
thanks for the replies both of you, if a XML file is the best way to go i think ill try that one out.

Would you be able to help me out with a simple example?

Else ill try read up on it.
 
You'll likely want to make this slightly more robust (add exception handling, checking for and preventing multiple key values, etc...) but here is a quick ini file reader/writer:

Code:
public function GetSetting(Key as string) as string
  dim ReturnVal as string = string.emptry
  dim dsSettings as new dataset
  if system.io.file.exists("Settings.ini") then _
    dsSettings.readxml("Settings.ini")

  dim dr() = dssettings.tables(0).select("Key = '" & key & "'")
  if dr.length = 1 then
    returnVal = dr(0)("Value")
  end if
end function

public sub SetSetting(Key as string, Value as string)
  dim dsSettigns as new dataset
  if system.io.file.exists("Settings.ini") then 
    dsSettings.readxml("Settings.ini")
  else
    dssettings.tables.add("Settings")
    dssettings.tables(0).columns.add("Key",gettype(string))
    dssettings.tables(0).columns.add("Value",gettype(string))
  end if

  dim dr() = dssettings.tables(0).select("Key = '" & key & "'")
  if dr.length = 1 then
    dr(0)("Value") = value
  else
    dim drSetting as datarow = dssettings.table(0).newrow
    drSetting("Key") = key
    drSetting("Value") = value
    dsSetting.Table(0).rows.add(drsetting)
  end if
  dssetting.writeXML("Settings.ini")
end sub

I did not compile this code, so it likely has a handful of error, but it should be enough to get you onto the right path.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
We use INI files, and we found NIni from Sourceforge.net to work well for us.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top