Results 1 to 6 of 6

Thread: [RESOLVED] saving, reading to a txt file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    MN
    Posts
    362

    Resolved [RESOLVED] saving, reading to a txt file

    I've figured out how to save lines to a txt file,
    Code:
    Dim data As String() = {TextBox1.Text, TextBox2.Text}
            IO.File.WriteAllLines("C:\Users\Me\Downloads\test2.txt", data)
    Now, to Open the file and display the text1 info in TextBox1 and text2 info in TextBox2??

    Cant seem to get it to work

  2. #2
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: saving, reading to a txt file

    This loads both of them at once since you have saved them both into the same file. Would need to know what the content of the textboxes was in order to separate the two into different textboxes when loading.
    vb.net Code:
    1. TextBox1.text = IO.File.ReadAllText("C:\Users\Me\Downloads\test2.txt")

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: saving, reading to a txt file

    Try this:

    Code:
    dim lines() as string = IO.File.ReadAllLines("C:\Users\Me\Downloads\test2.txt")
    TextBox1.Text = lines.firstordefault
    TextBox2.Text = lines.lastordefault

  4. #4
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: saving, reading to a txt file

    .paul.'s code works as long as you are not using multi line textboxes.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: saving, reading to a txt file

    if you are using multiline textboxes, you need to delimit the text:

    Code:
    IO.File.WriteAllText("C:\Users\Me\Downloads\test2.txt", TextBox1.Text & "|" & TextBox2.Text)
    then:

    Code:
    dim lines() as string = IO.File.ReadAllText("C:\Users\Me\Downloads\test2.txt").Split("|"c)
    TextBox1.Text = lines.firstordefault
    TextBox2.Text = lines.lastordefault

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    MN
    Posts
    362

    Re: saving, reading to a txt file

    thanks, got 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