I just got a textfile with a couple of numbers (1 number in one row), how would I sum these numbers? I thought it would be easy, but thing is it's turning out to be not so easy.
Printable View
I just got a textfile with a couple of numbers (1 number in one row), how would I sum these numbers? I thought it would be easy, but thing is it's turning out to be not so easy.
Read all lines of the file to an array, loop trough the array and add the numbers...
vb.net Code:
Dim total As Double = 0 For Each line As String In IO.File.ReadAllLines("PATH_TO_FILE") total += Double.Parse(line) Next MsgBox(String.Format("Sum={0}", total.ToString), MsgBoxStyle.Information)
Try this:
vb.net Code:
Dim fileContents() As String = IO.File.ReadAllLines("C:\Temp\test.txt") Dim sum As Long = (From item In fileContents Select CLng(item)).Sum MsgBox(sum)
Both codes work perfectly, big thanks to you guys, really helped me.