Benutzer mit den meisten Antworten
Text aus txt datei?

Frage
-
Hallo,
Wie kann ich den Inhalt einer txt-datei von dem Wort Start bis zu ende auslesen lassen?
Hier ist meine txt-box:
[START] Programm1, C:\Users\Tim\Desktop\Installer.exe; Programm2, C:\Users\Tim\Desktop\Entpacker.exe; Programm3, C:\Users\Tim\Desktop\Starter.exe; [ENDE]
Ich habe eine Form mit einer combobox, welche diese txt-datei auslesen soll und anschließend jede Zeile zwischen Start und Ende als neues Item annehmen soll... Wie muss ich vorgehen um diese Zeilen auszulesen und anschließend jede EINZELNE Zeile als ein NEUES item der combobox hinzuzufügen?
Antworten
-
Hier mal eine kleine Demo für einen der möglichen Lösungswege
Public Class Form1 Private cb As New ComboBox With {.Dock = DockStyle.Top} Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.Controls.Add(cb) Using rdr As New IO.StreamReader("TextFile1.txt") Dim wasStart As Boolean = False Do If rdr.EndOfStream Then Exit Do Dim zeile = rdr.ReadLine If zeile.StartsWith("[START]") Then wasStart = True Continue Do End If If zeile.StartsWith("[ENDE]") Then Exit Do If wasStart Then cb.Items.Add(zeile) Loop End Using End Sub End Class
--
Viele Gruesse
Peter- Als Antwort markiert Fearoy Freitag, 17. August 2012 11:45
-
Hi Fearoy,
benutze den StreamReader und ReadLine.
Aus der MSDN:
Class Test Public Shared Sub Main() Try ' Create an instance of StreamReader to read from a file. Dim sr As StreamReader = New StreamReader("TestFile.txt") Dim line As String ' Read and display the lines from the file until the end ' of the file is reached. Do line = sr.ReadLine() Console.WriteLine(Line) Loop Until line Is Nothing sr.Close() Catch E As Exception ' Let the user know what went wrong. Console.WriteLine("The file could not be read:") Console.WriteLine(E.Message) End Try End Sub End Class
- Als Antwort markiert Fearoy Freitag, 17. August 2012 11:45
Alle Antworten
-
Hier mal eine kleine Demo für einen der möglichen Lösungswege
Public Class Form1 Private cb As New ComboBox With {.Dock = DockStyle.Top} Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.Controls.Add(cb) Using rdr As New IO.StreamReader("TextFile1.txt") Dim wasStart As Boolean = False Do If rdr.EndOfStream Then Exit Do Dim zeile = rdr.ReadLine If zeile.StartsWith("[START]") Then wasStart = True Continue Do End If If zeile.StartsWith("[ENDE]") Then Exit Do If wasStart Then cb.Items.Add(zeile) Loop End Using End Sub End Class
--
Viele Gruesse
Peter- Als Antwort markiert Fearoy Freitag, 17. August 2012 11:45
-
Hi Fearoy,
benutze den StreamReader und ReadLine.
Aus der MSDN:
Class Test Public Shared Sub Main() Try ' Create an instance of StreamReader to read from a file. Dim sr As StreamReader = New StreamReader("TestFile.txt") Dim line As String ' Read and display the lines from the file until the end ' of the file is reached. Do line = sr.ReadLine() Console.WriteLine(Line) Loop Until line Is Nothing sr.Close() Catch E As Exception ' Let the user know what went wrong. Console.WriteLine("The file could not be read:") Console.WriteLine(E.Message) End Try End Sub End Class
- Als Antwort markiert Fearoy Freitag, 17. August 2012 11:45