Results 1 to 10 of 10

Thread: StreamWriter in vb.net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    26

    StreamWriter in vb.net

    Hello,I try to add new line in existing .txt but i want to add new line in end of existing line
    for example my settings.txt file have
    Line 1 = YES
    Line 2 = NO
    i want to add next line Here

    but not working because he write in first line not in the end of existing line.

    Thank you.
    Code:
      Using sw As StreamWriter = New StreamWriter(Application.StartupPath & "\settings.txt")
                   sw.WriteLine("LINE 3 ")
                                sw.Close()

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,426

    Re: StreamWriter in vb.net

    Hi Johan,

    You need to tell your ScreenWriter to write Lines 1 and 2 first... Then write Line3.

    This may involve reading Lines 1 and 2 first, e.g. 'read a line, write a line' (next) in a loop.
    Then 'write line3'.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

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

    Re: StreamWriter in vb.net

    Here's how to append...

    Code:
    Using sw As IO.StreamWriter = New IO.StreamWriter(Application.StartupPath & "\settings.txt", True)
        sw.WriteLine("LINE 3 ")
    End Using

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: StreamWriter in vb.net

    I wouldn't even use a StreamWriter. Generally speaking, only use a StreamWriter if you need to perform other operations between writes. The File.AppendAllLines method will append one or more lines to an existing file in a single line of code, e.g.
    vb.net Code:
    1. File.AppendAllLines(Path.Combine(Application.StartupPath, "settings.txt"), {"LINE 3 "})
    This is the equivalent of .paul.'s code withy the append parameter set to True. Calling WriteAllLines would be equivalent to the code you posted originally.

    Note the "proper" way to build a file path used there. Note also that the second argument to AppendAllLines is an array, even if you only want to append one line.

    This code assumes that the file already ends with a line break, but so does any code that uses a StreamWriter. If it might not then you will have to explicitly check and add one yourself somehow. That could be done by having an empty String as the first element of the array. This also will also append a line break after the last new line, just as use StreamWriter.WriteLine would, so you wouldn't have to check for a trailing line break after using it..

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

    Re: StreamWriter in vb.net

    @jm - Your code also assumes that the user is using >=VS2012

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: StreamWriter in vb.net

    Quote Originally Posted by .paul. View Post
    @jm - Your code also assumes that the user is using >=VS2012
    That is true, and that should be a valid assumption for most people. There aren't many valid reasons to be using a version of VS even as old as 2012, although I will say that many schools are living in the past and are still using very old versions, despite newer versions still being free. It was rather silly of Microsoft to introduce WriteAllLines, WriteAllText and AppendAllText in .NET 2.0 but wait until .NET 4.0 to introduce AppendAllLines.

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

    Re: StreamWriter in vb.net

    Quote Originally Posted by jmcilhinney View Post
    That is true, and that should be a valid assumption for most people. There aren't many valid reasons to be using a version of VS even as old as 2012, although I will say that many schools are living in the past and are still using very old versions, despite newer versions still being free. It was rather silly of Microsoft to introduce WriteAllLines, WriteAllText and AppendAllText in .NET 2.0 but wait until .NET 4.0 to introduce AppendAllLines.
    I'm using 08,12,15,17,19, and 22, though 22 isn't too user friendly on my workstation. I write most of my examples in VB2008 as nearly everyone is using a version that that will work with...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: StreamWriter in vb.net

    Quote Originally Posted by .paul. View Post
    I write most of my examples in VB2008 as nearly everyone is using a version that that will work with...
    That's fair enough, but you also deny users of newer versions better solutions. Probably the best option is to provide the optimal solution but mention if it requires later versions, then include an alternative for those still living in the last. I think I might make a point of doing that in future.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: StreamWriter in vb.net

    Quote Originally Posted by .paul. View Post
    I'm using 08,12,15,17,19, and 22, though 22 isn't too user friendly on my workstation.
    Note that you can also specify the VB language version in a newer IDE if you want to make sure that you only support language features up to a specific version.

    https://stackoverflow.com/questions/...al-studio-2015

    That would nix the need to use different IDE versions. You could probably get by with just 2019 and 2022, unless you need 2008 for CF development.

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

    Re: StreamWriter in vb.net

    Quote Originally Posted by jmcilhinney View Post
    Note that you can also specify the VB language version in a newer IDE if you want to make sure that you only support language features up to a specific version.

    https://stackoverflow.com/questions/...al-studio-2015

    That would nix the need to use different IDE versions. You could probably get by with just 2019 and 2022, unless you need 2008 for CF development.
    Thanks for the info...

Tags for this Thread

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