Guest User

Untitled

a guest
Jul 15th, 2014
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.18 KB | None | 0 0
  1.     Private Sub ParseIniFile()
  2.         Dim currentSection As String
  3.         Dim Id As Integer = 0
  4.  
  5.         Dim setting As Setting
  6.         Dim newSettings = New List(Of Setting)()
  7.  
  8.         Dim sectionQuery = From item In IO.File.ReadAllLines(appIniLocation)
  9.  
  10.         'example line (first setting from app.ini)
  11.         'allowHardwareStreams=1                      ; try to use hardware streams if available
  12.  
  13.  
  14.         'find section headers
  15.         i = 0
  16.         For Each item In sectionQuery
  17.             If item.StartsWith("[") Then
  18.                 ReDim Preserve sectionHeaders(i)
  19.                 Dim itemSplit() As String = Split(item, "[")
  20.                 Dim itemSplit2() As String = Split(itemSplit(1), "]")
  21.  
  22.                 sectionHeaders(i) = itemSplit2(0)
  23.                 i += 1
  24.             End If
  25.         Next
  26.  
  27.         'creating list of settings (the object)
  28.         i = 0
  29.         For Each line In sectionQuery
  30.             setting = Nothing
  31.             setting = New Setting()
  32.  
  33.             If line = "" Then
  34.                 'this means we're in a new section (it should anyways)
  35.                 i += 1
  36.             ElseIf line.StartsWith("[") Then
  37.                 'this will be the section name for the next group of settings
  38.                 currentSection = sectionHeaders(i)
  39.             Else
  40.                 'split line to more easily choose what we want
  41.                 Dim x1() As String = Split(line, "=")   'setting name -> x1(0)
  42.                 Dim x2() As String = Split(x1(1), " ")  'setting value -> x2(0)
  43.                 Dim x3() As String = Split(x1(1), ";")  'setting description -> x3(1)
  44.  
  45.                 setting.Id = Id
  46.                 setting.Section = currentSection
  47.                 setting.Name = x1(0)
  48.                 setting.Value = x2(0)
  49.                 setting.Description = x3(1)
  50.  
  51.                 Id += 1
  52.             End If
  53.             newSettings.Add(setting)
  54.         Next 'Program stops here (after completing the for loop) for some reason, not entirely sure why
  55.         'This line and those after it are never reached
  56.  
  57.         'replace old list and populate with new
  58.         settings.Clear()
  59.         settings.AddRange(newSettings)
  60.  
  61.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment