add a number to each line?

SecureECS

New member
Joined
Oct 23, 2009
Messages
3
Programming Experience
Beginner
Thank alot mate. Now i need to add a number to each line.
VB.NET:
        If File.Exists("C:\adresa.txt") Then
            Dim file1Sr As New StreamReader("C:\adresa.txt")
            Dim theLineNumber As Integer = 1
            Dim aLine As String
            Dim fileSw As StreamWriter
            fileSw = File.AppendText("C:\adresa.txt")
            While file1Sr.Peek() <> -1
                aLine = file1Sr.ReadLine
                If aLine.Length >= 0 Then
                    fileSw.WriteLine(theLineNumber.ToString& " - " & aLine)
                    theLineNumber += 1
                End If
            End While

            file1Sr.Close()
            fileSw.Close()
      End If
why thos code will not add a number to each line from text file ?
 
Last edited:
VB.NET:
Dim lines() As String = IO.File.ReadAllLines("adresa.txt")
For i As Integer = 0 To lines.Length - 1
    lines(i) = String.Format("{0} - {1}", i + 1, lines(i))
Next
IO.File.WriteAllLines("adresa.txt", lines)
 
Back
Top