Results 1 to 8 of 8

Thread: Write and Read txt Files

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    32

    Wink Write and Read txt Files

    How can I make txt files and read them in VB2008? The text file does not just consist of one single line. But probably more than a single line.

    How do I read the txt file and put each line in one variable.

    For example, the txt file:
    Code:
    Jimmy
    William
    Steph
    Stephen
    Flo
    And read it, putting each name in an array variable.
    Thanks!

  2. #2
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    94

    Re: Write and Read txt Files

    lol exact same thing i'm looking for.
    can't sort it out yet. wish it was as easy as in php.
    "Against All Odds"

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Write and Read txt Files

    Have either of you considered searching this forum or just doing a general google search?

    There are literally hundreds of people have already asked the question on here and been given examples and help, and a google search using the keywords "reading and writing text files vb.net" returned 1.2 million pages.

  4. #4
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    94

    Re: Write and Read txt Files

    It's like this

    Dim line As New List(Of String)
    line.AddRange(IO.File.OpenText("C:\hi.txt").ReadToEnd.Split(Environment.NewLine))
    "Against All Odds"

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    32

    Re: Write and Read txt Files

    Thx Choller, that one worked out.

    @Paul
    Yes, I've searched through Google and even YouTube. But all I find is to make one single file with one Line on it. Considering to search on this forum, I did found a lot of examples, but those examples does not fit to my use.

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Write and Read txt Files

    Yes, I've searched through Google and even YouTube. But all I find is to make one single file with one Line on it.
    OK I went back to my google search using the keywords I described above, and looked at the first two results. Both showed explicitly how to create a text file and in both occasions showed examples of writing multiple lines of data. Even if they hadn't and you have an example showing you how to write one line of data it isn't exactly a huge leap to work out how to write two lines.

    I'm not going to go through the other 1,209,998 pages returned in the results but I'm prepared to bet that most of them (if not all) would tell you exactly what you need to do.

  7. #7
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    94

    Re: Write and Read txt Files

    To be honest I googled to and I couldn't find a really simple solution. Everything was wrapped around some piece of extra code and it wasn't easy to figure it out.

    After coming back to this forum and using the search functions here I was able to find it.
    "Against All Odds"

  8. #8

    Re: Write and Read txt Files

    vb Code:
    1. dim read as system.io.streamreader = my.computer.filesystem.opentextfilereader("C:/myfile.txt")
    2. dim text = read.readtoend()

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