Ungetestete Ini klasse

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • Ungetestete Ini klasse

      hier mal meine ungetestete klasse um .ini dateien zu lesen und zu schreiben

      Quellcode

      1. ''' <summary>
      2. ''' Ini Klasse
      3. ''' </summary>
      4. ''' <remarks></remarks>
      5. Public Class Ini
      6. Private Fehler As String
      7. Private Sections As List(Of Section)
      8. Private EntryKeys As List(Of Key)
      9. Private Structure Key
      10. Dim SectionID As Byte
      11. Dim Name As String
      12. Dim Value As String
      13. Dim IsString As Boolean
      14. End Structure
      15. Enum KeyType
      16. Str
      17. Num
      18. End Enum
      19. Private Structure Section
      20. Dim Name As String
      21. Dim ID As Byte
      22. End Structure
      23. Private Function GenerateSectionID() As Byte
      24. For i As Integer = 0 To 255
      25. Dim Exsits As Boolean = False
      26. For Each s As Section In Sections
      27. If s.ID = i Then
      28. Exsits = True
      29. End If
      30. Next
      31. If Exsits = False Then
      32. Return i
      33. Exit For
      34. End If
      35. Next
      36. End Function
      37. Dim F As String
      38. Enum Modus
      39. Open
      40. Create
      41. End Enum
      42. Dim m As Modus
      43. ''' <summary>
      44. ''' Ini Datei öffnen und Lesen
      45. ''' </summary>
      46. ''' <param name="FileName">Der Pfad zur Ini Datei</param>
      47. ''' <param name="Mode">Ob neu erstellt oder geöffnet werden soll</param>
      48. ''' <remarks></remarks>
      49. Sub New(ByVal FileName As String, ByVal Mode As Modus)
      50. m = Mode
      51. Sections = New List(Of Section)
      52. EntryKeys = New List(Of Key)
      53. If Mode = Modus.Open Then
      54. Dim ActualSectionID As Byte = 0
      55. For Each l As String In IO.File.ReadAllLines(FileName)
      56. l = l.Trim
      57. If l.StartsWith("[") And l.EndsWith("]") Then
      58. Dim s As New Section
      59. s.ID = GenerateSectionID()
      60. s.Name = l.Remove(0, 1).Substring(0, l.Remove(0, 1).Count - 1)
      61. ActualSectionID = s.ID
      62. Sections.Add(s)
      63. Else
      64. Dim s() As String = l.Split("=")
      65. Dim k As Key = New Key
      66. k.SectionID = ActualSectionID
      67. k.Name = s(0).Trim
      68. Dim val As String = s(1).Trim
      69. If val.StartsWith(Chr(34)) And val.EndsWith(Chr(34)) Then
      70. k.Value = val.Remove(0, 1).Substring(0, val.Remove(0, 1).Count - 1)
      71. k.IsString = True
      72. Else
      73. k.Value = val
      74. k.IsString = False
      75. End If
      76. EntryKeys.Add(k)
      77. End If
      78. Next
      79. F = FileName
      80. Else
      81. F = FileName
      82. End If
      83. End Sub
      84. ''' <summary>
      85. ''' Eintrag Lesen
      86. ''' </summary>
      87. ''' <param name="SectionName">Der Name der Sektion in der der Eintrag gespeichert ist</param>
      88. ''' <param name="KeyName">Der Name des Eintrags</param>
      89. ''' <param name="Standart">Wird angezeigt wenn kein Wert gefunden wurde</param>
      90. Function Read(ByVal SectionName As String, ByVal KeyName As String, ByVal Standart As String) As String
      91. Dim sid As Byte
      92. Dim Found As Boolean = False
      93. For Each s As Section In Sections
      94. If s.Name = SectionName Then
      95. Found = True
      96. sid = s.ID
      97. Exit For
      98. End If
      99. Next
      100. If Found = False Then
      101. Fehler = "Selection nicht gefunden"
      102. Return "Error occoured"
      103. Exit Function
      104. End If
      105. Dim val As String = Standart
      106. For Each k As Key In EntryKeys
      107. If k.Name = KeyName And k.SectionID = sid Then
      108. If k.Value = "" Then
      109. val = Standart
      110. Else
      111. val = k.Value
      112. End If
      113. Exit For
      114. End If
      115. Next
      116. Fehler = ""
      117. Return val
      118. End Function
      119. ''' <summary>
      120. ''' Eintrag schreiben
      121. ''' </summary>
      122. ''' <param name="SectionName">Der Name der Sektion</param>
      123. ''' <param name="Keyname">Der Name des Eintrags</param>
      124. ''' <param name="Value">Der Wert der eingetragen wird</param>
      125. Function Write(ByVal SectionName As String, ByVal Keyname As String, ByVal Value As Object, ByVal IsString As Boolean) As Boolean
      126. Dim sid As Byte
      127. Dim Found As Boolean = False
      128. For Each s As Section In Sections
      129. If s.Name = SectionName Then
      130. Found = True
      131. sid = s.ID
      132. Exit For
      133. End If
      134. Next
      135. If Found = False Then
      136. Fehler = "Selection nicht gefunden"
      137. Return False
      138. Exit Function
      139. End If
      140. Found = False
      141. For x As Integer = 0 To EntryKeys.Count - 1
      142. If EntryKeys(x).SectionID = sid And EntryKeys(x).Name = Keyname Then
      143. Dim k As Key = New Key
      144. Found = True
      145. k.Name = Keyname
      146. k.Value = Value
      147. If IsString = True Then
      148. k.IsString = True
      149. End If
      150. EntryKeys.RemoveAt(x)
      151. EntryKeys.Add(k)
      152. End If
      153. Next
      154. If Not Found Then
      155. Dim k As New Key
      156. k.SectionID = sid
      157. k.Name = Keyname
      158. k.Value = Value
      159. EntryKeys.Add(k)
      160. End If
      161. Fehler = ""
      162. Return True
      163. End Function
      164. ''' <summary>
      165. ''' Ini Datei speichern
      166. ''' </summary>
      167. ''' <remarks></remarks>
      168. Function Flush() As Boolean
      169. Try
      170. Dim text As String = ""
      171. For Each s As Section In Sections
      172. Dim sid As Byte = s.ID
      173. text += "[" + s.Name + "]" + vbNewLine
      174. For Each k As Key In EntryKeys
      175. If k.SectionID = sid Then
      176. If k.IsString Then
      177. text += k.Name + " = " + Chr(34) + k.Value + Chr(34) + vbNewLine
      178. Else
      179. text += k.Name + " = " + k.Value + vbNewLine
      180. End If
      181. End If
      182. Next
      183. Next
      184. IO.File.WriteAllText(F, text)
      185. Fehler = ""
      186. Return True
      187. Catch ex As Exception
      188. Fehler = ex.Message
      189. Return False
      190. End Try
      191. End Function
      192. ''' <summary>
      193. ''' Erstellen von Sektionen
      194. ''' </summary>
      195. ''' <param name="Name">Der Name der Selektion</param>
      196. ''' <remarks>Sektion Erstellen</remarks>
      197. Function CreateSection(ByVal Name As String) As Boolean
      198. For Each s As Section In Sections
      199. If s.Name = Name Then
      200. Fehler = "Selection exsistiert bereits"
      201. Return False
      202. Exit Function
      203. End If
      204. Next
      205. Dim se As New Section
      206. se.Name = Name
      207. se.ID = GenerateSectionID()
      208. Sections.Add(se)
      209. Fehler = ""
      210. Return True
      211. End Function
      212. ''' <summary>
      213. ''' Sektion Löschen
      214. ''' </summary>
      215. ''' <param name="Name">Der Name der Sektion die gelöscht werden soll</param>
      216. ''' <remarks>Das Löschen von Sektionen</remarks>
      217. Function DeleteSection(ByVal Name As String) As Boolean
      218. For Each s As Section In Sections
      219. If s.Name = Name Then
      220. Sections.Remove(s)
      221. Fehler = ""
      222. Return True
      223. Exit Function
      224. End If
      225. Next
      226. Fehler = "Sektion existiert nicht"
      227. Return False
      228. End Function
      229. ''' <summary>
      230. ''' Gibt den fehler zurück
      231. ''' </summary>
      232. ''' <returns>Der letzte Fehker</returns>
      233. ''' <remarks></remarks>
      234. Function GetError() As String
      235. Return Fehler
      236. End Function
      237. End Class
      Alles anzeigen


      Anwendung:

      Quellcode

      1. Dim Inifile As New Ini(My.Application.Info.DirectoryPath + "\test.ini", Ini.Modus.Create) 'Inifile erstellen
      2. If Not Inifile.CreateSection("Test") Then Console.WriteLine(Inifile.GetError) : Console.Read() : End ' Neue Sektion erstellen
      3. If Not Inifile.Write("Test", "key1", "val", Ini.KeyType.Str) Then Console.WriteLine(Inifile.GetError) : Console.Read() : End 'Einen Key schreiben
      4. Inifile.Flush() 'Datei Speichern
      5. Console.WriteLine(Inifile.Read("Test", "key1", "NotFound")) 'Wert auslesen
      6. Console.ReadKey()
      7. Inifile.Release() 'Variablen löschen
      8. Inifile = Nothing 'Löschen

      Habs getestet funktioniert
    • Werbung zur Unterstützung des Forums ( Bitte AddBlocker deaktivieren )