hier mal meine ungetestete klasse um .ini dateien zu lesen und zu schreiben
Alles anzeigen
Anwendung:
Habs getestet funktioniert
Quellcode
- ''' <summary>
- ''' Ini Klasse
- ''' </summary>
- ''' <remarks></remarks>
- Public Class Ini
- Private Fehler As String
- Private Sections As List(Of Section)
- Private EntryKeys As List(Of Key)
- Private Structure Key
- Dim SectionID As Byte
- Dim Name As String
- Dim Value As String
- Dim IsString As Boolean
- End Structure
- Enum KeyType
- Str
- Num
- End Enum
- Private Structure Section
- Dim Name As String
- Dim ID As Byte
- End Structure
- Private Function GenerateSectionID() As Byte
- For i As Integer = 0 To 255
- Dim Exsits As Boolean = False
- For Each s As Section In Sections
- If s.ID = i Then
- Exsits = True
- End If
- Next
- If Exsits = False Then
- Return i
- Exit For
- End If
- Next
- End Function
- Dim F As String
- Enum Modus
- Open
- Create
- End Enum
- Dim m As Modus
- ''' <summary>
- ''' Ini Datei öffnen und Lesen
- ''' </summary>
- ''' <param name="FileName">Der Pfad zur Ini Datei</param>
- ''' <param name="Mode">Ob neu erstellt oder geöffnet werden soll</param>
- ''' <remarks></remarks>
- Sub New(ByVal FileName As String, ByVal Mode As Modus)
- m = Mode
- Sections = New List(Of Section)
- EntryKeys = New List(Of Key)
- If Mode = Modus.Open Then
- Dim ActualSectionID As Byte = 0
- For Each l As String In IO.File.ReadAllLines(FileName)
- l = l.Trim
- If l.StartsWith("[") And l.EndsWith("]") Then
- Dim s As New Section
- s.ID = GenerateSectionID()
- s.Name = l.Remove(0, 1).Substring(0, l.Remove(0, 1).Count - 1)
- ActualSectionID = s.ID
- Sections.Add(s)
- Else
- Dim s() As String = l.Split("=")
- Dim k As Key = New Key
- k.SectionID = ActualSectionID
- k.Name = s(0).Trim
- Dim val As String = s(1).Trim
- If val.StartsWith(Chr(34)) And val.EndsWith(Chr(34)) Then
- k.Value = val.Remove(0, 1).Substring(0, val.Remove(0, 1).Count - 1)
- k.IsString = True
- Else
- k.Value = val
- k.IsString = False
- End If
- EntryKeys.Add(k)
- End If
- Next
- F = FileName
- Else
- F = FileName
- End If
- End Sub
- ''' <summary>
- ''' Eintrag Lesen
- ''' </summary>
- ''' <param name="SectionName">Der Name der Sektion in der der Eintrag gespeichert ist</param>
- ''' <param name="KeyName">Der Name des Eintrags</param>
- ''' <param name="Standart">Wird angezeigt wenn kein Wert gefunden wurde</param>
- Function Read(ByVal SectionName As String, ByVal KeyName As String, ByVal Standart As String) As String
- Dim sid As Byte
- Dim Found As Boolean = False
- For Each s As Section In Sections
- If s.Name = SectionName Then
- Found = True
- sid = s.ID
- Exit For
- End If
- Next
- If Found = False Then
- Fehler = "Selection nicht gefunden"
- Return "Error occoured"
- Exit Function
- End If
- Dim val As String = Standart
- For Each k As Key In EntryKeys
- If k.Name = KeyName And k.SectionID = sid Then
- If k.Value = "" Then
- val = Standart
- Else
- val = k.Value
- End If
- Exit For
- End If
- Next
- Fehler = ""
- Return val
- End Function
- ''' <summary>
- ''' Eintrag schreiben
- ''' </summary>
- ''' <param name="SectionName">Der Name der Sektion</param>
- ''' <param name="Keyname">Der Name des Eintrags</param>
- ''' <param name="Value">Der Wert der eingetragen wird</param>
- Function Write(ByVal SectionName As String, ByVal Keyname As String, ByVal Value As Object, ByVal IsString As Boolean) As Boolean
- Dim sid As Byte
- Dim Found As Boolean = False
- For Each s As Section In Sections
- If s.Name = SectionName Then
- Found = True
- sid = s.ID
- Exit For
- End If
- Next
- If Found = False Then
- Fehler = "Selection nicht gefunden"
- Return False
- Exit Function
- End If
- Found = False
- For x As Integer = 0 To EntryKeys.Count - 1
- If EntryKeys(x).SectionID = sid And EntryKeys(x).Name = Keyname Then
- Dim k As Key = New Key
- Found = True
- k.Name = Keyname
- k.Value = Value
- If IsString = True Then
- k.IsString = True
- End If
- EntryKeys.RemoveAt(x)
- EntryKeys.Add(k)
- End If
- Next
- If Not Found Then
- Dim k As New Key
- k.SectionID = sid
- k.Name = Keyname
- k.Value = Value
- EntryKeys.Add(k)
- End If
- Fehler = ""
- Return True
- End Function
- ''' <summary>
- ''' Ini Datei speichern
- ''' </summary>
- ''' <remarks></remarks>
- Function Flush() As Boolean
- Try
- Dim text As String = ""
- For Each s As Section In Sections
- Dim sid As Byte = s.ID
- text += "[" + s.Name + "]" + vbNewLine
- For Each k As Key In EntryKeys
- If k.SectionID = sid Then
- If k.IsString Then
- text += k.Name + " = " + Chr(34) + k.Value + Chr(34) + vbNewLine
- Else
- text += k.Name + " = " + k.Value + vbNewLine
- End If
- End If
- Next
- Next
- IO.File.WriteAllText(F, text)
- Fehler = ""
- Return True
- Catch ex As Exception
- Fehler = ex.Message
- Return False
- End Try
- End Function
- ''' <summary>
- ''' Erstellen von Sektionen
- ''' </summary>
- ''' <param name="Name">Der Name der Selektion</param>
- ''' <remarks>Sektion Erstellen</remarks>
- Function CreateSection(ByVal Name As String) As Boolean
- For Each s As Section In Sections
- If s.Name = Name Then
- Fehler = "Selection exsistiert bereits"
- Return False
- Exit Function
- End If
- Next
- Dim se As New Section
- se.Name = Name
- se.ID = GenerateSectionID()
- Sections.Add(se)
- Fehler = ""
- Return True
- End Function
- ''' <summary>
- ''' Sektion Löschen
- ''' </summary>
- ''' <param name="Name">Der Name der Sektion die gelöscht werden soll</param>
- ''' <remarks>Das Löschen von Sektionen</remarks>
- Function DeleteSection(ByVal Name As String) As Boolean
- For Each s As Section In Sections
- If s.Name = Name Then
- Sections.Remove(s)
- Fehler = ""
- Return True
- Exit Function
- End If
- Next
- Fehler = "Sektion existiert nicht"
- Return False
- End Function
- ''' <summary>
- ''' Gibt den fehler zurück
- ''' </summary>
- ''' <returns>Der letzte Fehker</returns>
- ''' <remarks></remarks>
- Function GetError() As String
- Return Fehler
- End Function
- End Class
Anwendung:
Quellcode
- Dim Inifile As New Ini(My.Application.Info.DirectoryPath + "\test.ini", Ini.Modus.Create) 'Inifile erstellen
- If Not Inifile.CreateSection("Test") Then Console.WriteLine(Inifile.GetError) : Console.Read() : End ' Neue Sektion erstellen
- If Not Inifile.Write("Test", "key1", "val", Ini.KeyType.Str) Then Console.WriteLine(Inifile.GetError) : Console.Read() : End 'Einen Key schreiben
- Inifile.Flush() 'Datei Speichern
- Console.WriteLine(Inifile.Read("Test", "key1", "NotFound")) 'Wert auslesen
- Console.ReadKey()
- Inifile.Release() 'Variablen löschen
- Inifile = Nothing 'Löschen
Habs getestet funktioniert