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!

Collect unknown section names from INI File? 1

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
I thought I knew how to do this, but I can't find my example I stored away years ago...

I am writing a small app in VB.NET and it needs to be able to read data out of an INI file. Problem is, in this case, that the section names in the INI file are unknown until the app gets access to the file. The format of each section is the same (4 string values with thr same identifiers) but the section names are always different.

I thought there was a way using the API call GetPrivateProfileString to get the section names out of the file, so I could store them in an array and then go back and read out the vlaues for each section. But I can't find my example (everything I have indicates I need to know the section name ahead of time).

I suppose I could just use a data reader and parse out the contents of the file, looking for values within brackets, then do the GetPrivateProfileString operation once I knew the values, but I really would prefer a more direct way.

Can anyone help?
 
I use NIni (available from SourceForge) and it can list out all the sections.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks Chip, looks like a winner. I'll download it and have a look.

Craig
 
Use the IniDocument.Sections property to get a IniSectionCollection object which you can iterate through.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip,

Great tool, already saved me a bunch of trouble trying to get information out of the INI file in a different place. But I can't seem to find the IniDocument.Sections you mention. Can you be a bit more specific?

Thanks again!

Craig
 
If you open the NiniReference.chm file located in your Nini\Docs\Reference\chm directory, the first class in the help pane is IniDocument. You can create an instance of one by passing in the path to your .ini file:
Code:
Dim iDoc as New IniDocument("c:\myconfig.ini")
Dim iSections As IniSectionCollection
iSections = iDoc.Sections
Chip H.
(I might have some of the VB.NET syntax wrong, sorry)



____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip,

Thanks again -- your example was right on target.

One more question if you don't mind -- I want to use the NINI tool to also delete keys and sections from the Source INI file, but I can't seem to understand the tool well enough to quite figure it out. Can you show me an example of how to delete a key and a section?

Thanks so much, I'm really struggling trying to get used to all this new .NET stuff.
 
Once you've identified which section you want to delete, call the Remove method.
Code:
colSections.Remove("mysection")
myIniDocument.Save()
If you want to remove a key from a particular section, it's very similar.
Code:
colSections.Item("mysection").Remove("mykeyname")
myIniDocument.Save()
How the NIni library is organized is a a pretty standard design pattern that you'll see all over the place in .NET. Whenever you have multiples of something, it'll be in a collection. You use the Item property to select which object from the collection to work with, and once you've got that, you can do anything to that object that you want.

Get to know it. It'll be on the test.

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