Results 1 to 2 of 2

Thread: INI reader

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    2

    INI reader

    Hi every One this is my first post so be nice.

    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class INIReader
    4.     Public ReadOnly file As String
    5.  
    6.     Sub New(ByVal inifile As String)
    7.   If IO.File.Exists(inifile) Then
    8.     file = inifile
    9.   Else
    10.     'If our file isn't found, we want to let the user know.
    11.     Throw New FileNotFoundException("File does not exist.", inifile)
    12.   End If
    13.     End Sub
    14.  
    15.     Public Function GetValue(ByVal value As String, Optional ByVal section As String = Nothing) As String
    16.   Using reader As New IO.StreamReader(file)
    17.     Dim temp As String
    18.  
    19.     If section Is Nothing Then
    20.     While Not reader.EndOfStream
    21.     temp = reader.ReadLine().Trim
    22.  
    23.     'If the line is null then we don't need to read it
    24.     If temp Is Nothing Then Continue While
    25.  
    26.     'If the line is blank then we don't need to read it
    27.     If temp.Length = 0 Then Continue While
    28.  
    29.     'If the line is a comment then we don't need to read it
    30.     If temp(0) = ";"c Then Continue While
    31.  
    32.     'If the line is a section, then we don't need to read it
    33.     If temp(0) = "["c Then Continue While
    34.  
    35.     'If the line begins with the property name, then we return everything after the "=".
    36.     If temp.Substring(0, temp.IndexOf("="c)).Trim = value Then
    37.     Return temp.Substring(temp.IndexOf("="c) + 1).Trim
    38.     End If
    39.     End While
    40.  
    41.     'Either we got to the next section or the end of the file and nothing was found.
    42.     Return Nothing
    43.     Else
    44.     While Not reader.EndOfStream
    45.     temp = reader.ReadLine.Trim
    46.  
    47.     'If the line is null then we don't need to read it
    48.     If temp Is Nothing Then Continue While
    49.  
    50.     'If the line is blank then we don't need to read it
    51.     If temp.Length = 0 Then Continue While
    52.  
    53.     'If the line is a comment then we don't need to read it
    54.     If temp(0) = ";"c Then Continue While
    55.  
    56.     If temp.Substring(0, section.Length + 2) = "[" & section & "]" Then
    57.     'We've found the section, we need to clear the temp variable to use it for our property now
    58.     temp = Nothing
    59.  
    60.     While Not reader.EndOfStream
    61.     temp = reader.ReadLine().Trim
    62.  
    63.     'If the line is null then we don't need to read it
    64.     If temp Is Nothing Then Continue While
    65.  
    66.     'If the line is blank then we don't need to read it
    67.     If temp.Length = 0 Then Continue While
    68.  
    69.     'If the line is a comment then we don't need to read it
    70.     If temp(0) = ";"c Then Continue While
    71.  
    72.     'If the line is a section, then the section we were looking for didn't have our property, so we exit.
    73.     If temp(0) = "["c Then Exit While
    74.  
    75.     'If the line begins with the property name, then we return everything after the "=".
    76.     If temp.Substring(0, temp.IndexOf("="c)).Trim = value Then
    77.     Return temp.Substring(temp.IndexOf("="c) + 1).Trim
    78.     End If
    79.     End While
    80.     End If
    81.     End While
    82.  
    83.     'Either we got to the next section or the end of the file and nothing was found.
    84.     Return Nothing
    85.     End If
    86.  
    87.     temp = Nothing
    88.  
    89.     reader.Close()
    90.   End Using
    91.     End Function
    92.  
    93.     Public Function GetProperties(ByVal section As String) As List(Of String())
    94.   GetProperties = New List(Of String())
    95.  
    96.   Using reader As New IO.StreamReader(file)
    97.     Dim temp As String
    98.  
    99.     While Not reader.EndOfStream
    100.     temp = reader.ReadLine.Trim
    101.  
    102.     'If the line is null then we don't need to read it
    103.     If temp Is Nothing Then Continue While
    104.  
    105.     'If the line is blank then we don't need to read it
    106.     If temp.Length = 0 Then Continue While
    107.  
    108.     'If the line is a comment then we don't need to read it
    109.     If temp(0) = ";"c Then Continue While
    110.  
    111.     'If the string isn't even as long as "[" & section & "]" it's obviously not the section
    112.     If temp.Length < section.Length + 2 Then Continue While
    113.  
    114.     If temp.Substring(0, section.Length + 2) = "[" & section & "]" Then
    115.     'We've found the section, we need to clear the temp variable to use it for our property now
    116.     temp = Nothing
    117.  
    118.     While Not reader.EndOfStream
    119.     temp = reader.ReadLine().Trim
    120.  
    121.     'If the line is null then we don't need to read it
    122.     If temp Is Nothing Then Continue While
    123.  
    124.     'If the line is blank then we don't need to read it
    125.     If temp.Length = 0 Then Continue While
    126.  
    127.     'If the line is a comment then we don't need to read it
    128.     If temp(0) = ";"c Then Continue While
    129.  
    130.     'If the line is a section, then the section we were looking for didn't have our property, so we exit.
    131.     If temp(0) = "["c Then Exit While
    132.  
    133.     'If the line begins with the property name, then we return everything after the "=".
    134.     If temp.Substring(0, temp.IndexOf("="c)).Trim.Length <> 0 Then
    135.     GetProperties.Add({temp.Substring(0, temp.IndexOf("="c)).Trim, temp.Substring(temp.IndexOf("="c) + 1).Trim})
    136.     End If
    137.     End While
    138.     End If
    139.     End While
    140.  
    141.     'Either we got to the next section or the end of the file and nothing was found.
    142.     Return GetProperties
    143.  
    144.     temp = Nothing
    145.  
    146.     reader.Close()
    147.   End Using
    148.     End Function
    149.  
    150. End Class
    151.  
    152. Public Class INISection
    153.     Public properties As List(Of String())
    154.  
    155.     Sub New(ByVal reader As INIReader, ByVal section As String)
    156.   properties = reader.GetProperties(section)
    157.     End Sub
    158.  
    159.     Public Function GetValue(ByVal name As String)
    160.   Return properties.Find(Function(x) x(0) = name)(1)
    161.     End Function
    162. End Class

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    2

    Re: INI reader

    Sorry i cant seem to edit. This was written by an excellent coder on another forum so i thought i would share it. Works great with me. Enjoy guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width