Lese-Problem

  • VB.NET

Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von stefan15.

    Lese-Problem

    hi,
    folgendes Problem : ich möchte eine Datei auslesen, in der E-mail addressen stehen. Wenn ich nun aber die Datei bearbeite, ändert sich im Programm nichts....obwohl jedesmal beim Starten die DAtei eingelesen werden soll.
    ;( hat irgendjemad eine Idee??
    sry code vergessen ;) :

    VB.NET-Quellcode

    1. 'Kontaktdaten einlesen
    2. Dim t As Object
    3. Dim f As Object
    4. Dim FileLength As Object
    5. f = ("C:\Programme\Email\contacts.txt")
    6. FileOpen(1, f, OpenMode.Input)
    7. FileLength = LOF(1)
    8. t = InputString(1, FileLength)
    9. FileClose(1)
    10. RichTextBox1.Text = t
    11. 'Kontaktdaten aufsplitten und in die Liste übernehmen
    12. For i As Integer = 0 To RichTextBox1.Lines.Count - 1
    13. ListBox1.Items.Add(RichTextBox1.Lines(i))
    14. Next
    Versuchs mal mit File.Readalllines

    MSDN schrieb:


    This method opens a file, reads each line of the file, then adds each line as an element of a string array. It then closes the file. A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed.

    This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.IO
    3. Public Class Test
    4. Public Shared Sub Main()
    5. Dim path As String = "c:\temp\MyTest.txt"
    6. Dim sw As StreamWriter
    7. ' This text is added only once to the file.
    8. If File.Exists(path) = False Then
    9. ' Create a file to write to.
    10. Dim createText() As String = {"Hello", "And", "Welcome"}
    11. File.WriteAllLines(path, createText)
    12. End If
    13. ' This text is always added, making the file longer over time
    14. ' if it is not deleted.
    15. Dim appendText As String = "This is extra text" + Environment.NewLine
    16. File.AppendAllText(path, appendText)
    17. ' Open the file to read from.
    18. Dim readText() As String = File.ReadAllLines(path)
    19. Dim s As String
    20. For Each s In readText
    21. Console.WriteLine(s)
    22. Next
    23. End Sub
    24. End Class

    stefan15 schrieb:

    sry code vergessen ;) :

    VB.NET-Quellcode

    1. 'Kontaktdaten einlesen
    2. Dim t As Object
    3. Dim f As Object
    4. Dim FileLength As Object
    5. f = ("C:\Programme\Email\contacts.txt")
    6. FileOpen(1, f, OpenMode.Input)
    7. FileLength = LOF(1)
    8. t = InputString(1, FileLength)
    9. FileClose(1)
    10. RichTextBox1.Text = t
    11. 'Kontaktdaten aufsplitten und in die Liste übernehmen
    12. For i As Integer = 0 To RichTextBox1.Lines.Count - 1
    13. ListBox1.Items.Add(RichTextBox1.Lines(i))
    14. Next

    Bwaaaaaah. Ich habe vor 'ner halben Stunde einen Beitrag unter Tipps und Tricks gepostet, in dem erklärt wird, warum man alte VB6-Code-Relikte möglichst nicht mehr benutzen sollte und welche Alternativen es gibt. wurde leider noch nicht freigeschaltet...
    Auf jeden Fall ist diese Art auf Dateien zuzugreifen vollig veraltet. Schau dir mal System.IO.[...] an. Im Internet gibts auch genügend Anleitungen zum korrekten Zugriff auf Dateien.

    lg SeriTools
    | Keine Fragen per PN oder Skype.