Results 1 to 3 of 3

Thread: quick way to read in a text file.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    quick way to read in a text file.

    Hi,

    i'm reading in a text file in the following way:

    Code:
    dim sr as streamreader
    sr = file.opentext(filename)
    dim x as string
    while sr.peek <> -1
        x = trim(sr.Readline()) & vbcrlf
        txtFile.text = txtFile.text & x
    end while
    sr.close()
    Problem is it seems this way of reading file is sometimes quite slow? Is there a quicker way to read in a text file?

    Many thanks
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    either add it all in one go, rather than a loop like this....
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Button1_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] Button1.Click
    2.  
    3.         [Color=Blue]Dim[/COLOR] sr [Color=Blue]As[/COLOR] [Color=Blue]New[/COLOR] StreamReader([Color=Blue]New[/COLOR] FileStream("C:\test.txt", FileMode.Open))
    4.         txtFile.AppendText(sr.ReadToEnd)
    5.         sr.Close()
    6.  
    7.     [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
    or read through the lines using filestream to open it, like this...
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Button2_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] Button2.Click
    2.  
    3.         [Color=Blue]Dim[/COLOR] sr [Color=Blue]As[/COLOR] [Color=Blue]New[/COLOR] StreamReader([Color=Blue]New[/COLOR] FileStream("C:\test.txt", FileMode.Open))
    4.         [Color=Blue]While[/COLOR] [Color=Blue]Not[/COLOR] sr.Peek
    5.             txtFile.AppendText(sr.ReadLine & Environment.NewLine)
    6.         [Color=Blue]End[/COLOR] [Color=Blue]While
    7. [/COLOR]        sr.Close()
    8.  
    9.     [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    thanks,

    I'll give them a try!
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

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