reading .ini and showing settings

Alex_W

Member
Joined
Jan 5, 2009
Messages
22
Programming Experience
Beginner
I've been trying to get my head round a problem for days now, hope someone can help.

I want to read 'File.ini' line by line checking if specific text exists in the file and then display on a windows form weather particular settings are turned on or off.

So far this code works if "TargetText" exists but if it doenst I get an error on the code line: "If line.Contains("TargetText") Then".

VB.NET:
Using z As System.IO.StreamReader = New System.IO.StreamReader(File.ini")
            Dim line As String
            ' Read first line.
            line = z.ReadLine
            ' Loop over each line in file, While list is Not Nothing.
            Do While Not (line.Contains("TargetText"))
                ' Read in the next line.
                line = z.ReadLine
                If line.Contains("TargetText") Then
                    ' code to execute if the text exists in the file
                        Exit Do
                    End If
                Loop
        End Using

It works if "TargetText" existin in a line in the file but if it doesnt exist I get the 'Null Reference Exception. Object reference not set to an instance of an object.' Any suggestions greatfully recieved!
 
Last edited:
ReadLine help said:
The returned value is null (Nothing in Visual Basic) if the end of the input stream is reached.
You can't do line.Contains if line is Nothing. When end of stream is reached you have to stop reading. How about: Do Until z.EndOfStream ?

As for ini files in general I prefer accessing them as such, following the section/key/value format. If the file format has any meaning in your case you can for example try the IniFile class I posted here: http://www.vbdotnetforums.com/vb-net-general-discussion/21166-loading-ini-file.html
 
Thanks for the reply, the .ini file has blank spaces which seem to be ok and the error happens when the targettext doesnt exist. I guess its looping over and over, I have been told to use EndOfStream but the following code tells me that 'Contains' isnt a valid member of StreamReader???

VB.NET:
 Using z As System.IO.StreamReader = New System.IO.StreamReader(File.ini")
            Dim line As String
            Do While (z.EndOfStream = False)
                line = z.ReadLine
                If z.contains("targetText") Then
                    'do something with line
                    Exit Do
                End If
            Loop
        End Using

Anyone shed any light?
 
VB.NET:
, the .ini file has blank spaces which seem to be ok and the error happens when the targettext doesnt exist.
Empty strings is not a problem, the exception in previous code was caused by you reading passed the end of stream, which would of course set the line variable to Nothing, which with 100% certainty will throw NullReferenceException if you would try to call line.Contains(param) in that case. What is in param is not a problem as long as it is not a null reference (Nothing).
z.Contains ?
It is correct that StreamReader does not have a Contains method. String class has. line.Contains is fine with the new code loop, in this case line variable will never be Nothing.
 
Back
Top