Results 1 to 3 of 3

Thread: sum numbers in a text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    78

    sum numbers in a text file

    Hi,

    I'm trying to add the numbers in a text file and show the result on a label.
    Public Function GetInitialTime(ByVal TotalTime As Integer) As String

    Dim fileContents() As String = IO.File.ReadAllLines("\My Documents\my_Counter.txt")
    Dim sum As Long = (From item In fileContents Select CLng(item)).Sum

    TotalTime = sum
    Tot_Time = TotalTime

    Sec = TotalTime Mod 60
    Min = ((TotalTime - Sec) / 60) Mod 60
    Hrs = ((TotalTime - (Sec + (Min * 60))) / 3600)

    Return Format(Hrs, "000000") & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
    End Function
    For the mobile phone (win6.5) development it comes with an error:
    "ReadAllLines" is not a member of "System.IO.File"
    But if I create a development for a PC application and use the same code, it works fine.

    Can anybody help me?
    Or has anybody got another idea of how to add all lines (there are only numbers on each line) in my text file?

    Thanks.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    78

    Re: sum numbers in a text file

    If you're targetting Windows Mobile, you'll be using the compact framework. ReadAllLines is not supported in the compact framework.
    So, I've found out that my approach is a dead end.
    can anybody help me sum all the numbers in my text file somehow else?

    thanks.

  3. #3
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: sum numbers in a text file

    You can create your own function to read the entire file, have a look at here
    or use:
    Code:
    Private Function ReadAllLines() As String
    	Dim data As String = String.Empty
    	Try
    		Dim sr As New System.IO.StreamReader("\My Documents\my_Counter.txt")
    		data = sr.ReadToEnd()
    		sr.Close()
    	Catch generatedExceptionName As Exception
    		Throw
    	End Try
    	Return data
    End Function
    and split the ReadAllLines() result on separator vbnewline
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width