visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: Csharp :: .Net :: How to read an ini file in VB.Net ?


By: Nono Great Britain  Date: 26/05/2005 09:06:20  English  Points: 40 Status: Answered
Quality : Excellent
I designed a software that uses an INI file. I don't know the best way to read it. It may contain any keys but currently is set as :

[setup] DirectoryFiles = something file1 = file1.doc ' some comment file2 = file2.txt [settings] ConfigSetting = 1



thanks for your input
By: FiatLink Date: 26/05/2005 09:10:42 English  Type : Comment
I once used the INIReader class from mentalis.org

You can go to http://www.mentalis.org/soft/class.qpx?id=6 for getitng it.

I must state that currently, the hype is on using XML files in stead of the Registry (deprecated?) or INI files (deprecated for sure, since Win 95/NT4 came out )
By: Bernard Date: 26/05/2005 09:13:35 English  Type : Comment
If you have a fixed naming structure, you can use this kind of code :

Private INI As IniReader Private Files As New ArrayList() Private Directory as string Private sTemp as String private i as integer INI = New IniReader("yourfile.ini") Directory = INI.ReadString("setup", "DirectoryFiles") i=1 Do Try sTemp = INI.ReadString("setup", "file" & Trim(Str(i))) Catch Exit Do End Try If sTemp <> "" Then Files.Add(sTemp) i = i + 1 Else Exit Do End If Loop

By: Bernard Date: 26/05/2005 09:14:55 English  Type : Comment
Perhaps you'd better use an XML file, it is a lot better and quick.

Something like this :

Dim filetoread As DataSet = New DataSet filetoread .ReadXml((Application.StartupPath) & "\yourxmlfilename.xml", XmlReadMode.Auto) textbox1.Text = filetoread .Tables("table1").Rows(a).Item("Field1")



You have the XML look like this:

<?xml version="1.0" standalone="yes"?> <dataroot> <Table1> <Field1>whatever you want to type</field1> </Table1> </dataroot>

By: VGR Date: 26/05/2005 09:34:52 English  Type : Answer
Bernard, I have to totally disagree with you on this.

First of all, XML reading may be "quick" to write the code, but for sure it won't be "quick" to process (parse) the file !

Any XML parser around, optimized to the bones and the very last possible codebyte, has the following disavantages :
- very long overhead (when instanciating the parser)
- difficult configuration in case it's buggy about tags without closing tags etc
- VERY huge amount of memory consumed. As an example, using the standard Perl XML parser, parsing (not counting the reading time of the file and the normal amount of memory thus required) 30 MB of XML TEXT (yes, it's text after all) consumes 512 MB of RAM and takes minutes. I'm pretty sure the PHP or .Net one is exactly of the same kind.

Just imagine if I were to use this parser on the 200 MB XML files I have to handle ;-))

Now I'm using a parser I made myself to solve - partially - the memory starvation problem, but I MUST say that if the data had been stored as a CSV file (for instance) :
- filesize would go from 30 MB to 500 KB (XML is repeating all tags for each similar record - that's wasted diskspace, network bandwidth, and memory)
- email transmitting would be instantaneous
- processing time would go down ten times (at least). I could even import directly that file in a database table, add indices and be left with just using the data, in stead of losing my CPU time parsing & analysing the XML and then only write the actual data to a database table.

I have this opinion because my XML files are containing hundreds of thousands of similar records (like invoices lines), so having a CSV file would be perfect : one line to define the column names = tag names, and one line with the DATA of each record (NOT all tags again and again for each line !!! )

That's typically the case in EDI (electronic data interchange), and the sad story is that normalization authorities agreed on using XML for this kind of data transfer, which is of uttermost stupidity IMHO.

Thus, parsing an XML is SLOWER and MORE RAM GREEDY than any other method, and it is unacceptable in real world except on VERY SMALL files ( < 500 KB ).

In your case, I would consider the INI file as a normal text file and read it using fopen(), fgets(), fclose() and exploding each line into a valued pair (variable=value) when not a comment line.

That's the most flexible solution (you don't have to hardcode your INI file structure), and the most efficient one (no overhead, no memory required except the size of the buffer in fgets() - usually 2 KB but you could say 80 characters if that's good enough for you to contain a full line - don't forget the trailing CRLF counting as 2 characters - )

Best regards,

VGR

PS : one last note. You shouldn't mix case in your tag naming. XML standards require everything to be lowercase and your XML file ***should*** get rejected by any decent standards-compliant parser (not Microsoft's, apparently ;-)))

PPS : just in case you still want to stick (that's the word, "stick" as in "getting stuck on XML and not able to revert to faster normal text file reading" ;-)) to XML, then there is this open source class for you to look at.
By: Nono Date: 27/05/2005 08:23:38 English  Type : Comment
Agree. I just read the file as a text file and everything is ok. Thanks for having pushed me to NOT use XML ;-))
By: Bernard Date: 31/05/2005 08:56:39 English  Type : Comment
I prefer the "beauty" of XML structured documents. You're right about the criterium of the file size, though.

Do register to be able to answer

EContact
browser fav
page generated in 352.614880 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page