textdatei bestimmte zeile editieren klappt nicht

  • VB.NET

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von Monte.

    textdatei bestimmte zeile editieren klappt nicht

    hallo, ich habe vollenden code:

    VB.NET-Quellcode

    1. Public Sub EditLine(ByVal Str As String, ByVal Value As String, Optional ByVal Langname As String = "True")
    2. If IO.File.Exists(Pfad) Then
    3. If Langname = "True" Then
    4. Dim l As List(Of String)
    5. Dim ls = My.Computer.FileSystem.ReadAllText(Pfad).ToList()
    6. For Each o As String In ls
    7. If Not Regex.Match(o, "(?<str>.*?)=").Groups("str").Value = Str Then
    8. l.Add(o)
    9. ElseIf Regex.Match(o, "(?<str>.*?)=").Groups("str").Value = Str Then
    10. l.Add(Regex.Match(o, "(?<str>.*?)=").Groups("str").Value & Value)
    11. End If
    12. Next o
    13. 'My.Computer.FileSystem.WriteAllText(Pfad, Str & "=" & Value, True)
    14. IO.File.WriteAllLines(Pfad, l)
    15. RaiseEvent OnWriting(New LngEventargs(Str, Value))
    16. End If
    17. End If
    18. End Sub

    und es kommt ein Fehler bei: l.add(o)
    Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

    kann mir da jemand helfen???

    mfg filmee24
    ja habe ich, mein neuer code:

    VB.NET-Quellcode

    1. Public Sub EditLine(ByVal Str As String, ByVal Value As String, Optional ByVal Langname As String = "")
    2. If IO.File.Exists(Pfad) Then
    3. If Langname = "" Then
    4. Dim l As New List(Of String)
    5. Dim ls = My.Computer.FileSystem.ReadAllText(Pfad).ToList()
    6. For Each o As String In ls
    7. If Not Regex.IsMatch(Str, "(?<str>.*?)=") Then
    8. l.Add(o)
    9. ElseIf Regex.IsMatch(Str, "(?<str>.*?)=") Then
    10. l.Add(Regex.Match(o, "(?<str>.*?)=").Groups("str").Value & "=" & Value)
    11. MsgBox(Regex.Match(o, "(?<str>.*?)=").Groups("str").Value & "=" & Value)
    12. End If
    13. Next o
    14. ' My.Computer.FileSystem.WriteAllText(Pfad, Str & "=" & Value, True)
    15. IO.File.WriteAllLines(Pfad, l)
    16. RaiseEvent OnWriting(New LngEventargs(Str, Value))
    17. End If
    18. End If
    19. End Sub

    aber es passiert nichts?
    Ja, schau dir doch deinen Code an (siehe For Each)

    Vielleicht hilft dir das ist jetzt ausm Kopf im Editor vom Tablet

    VB.NET-Quellcode

    1. Private Sub EditLine(ByVal Path As String, ByVal Str As String, ByVal Value As String)
    2. If File.Exists(Path) Then
    3. Dim lines() As String = File.ReadAllLines(Path)
    4. For i As Integer = 0 To lines.Length - 1
    5. If Regex.IsMatch(lines(i), "Dein_Pattern") Then
    6. Regex.Replace(lines(i), "Dein_Pattern", Value)
    7. End If
    8. Next
    9. File.WriteAllLines("Dein_Pfad", lines)
    10. End If
    11. End Sub

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Monte“ ()