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
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
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:
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..
Re: StreamWriter in vb.net
@jm - Your code also assumes that the user is using >=VS2012
Re: StreamWriter in vb.net
Quote:
Originally Posted by
.paul.
@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.
Re: StreamWriter in vb.net
Quote:
Originally Posted by
jmcilhinney
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...
Re: StreamWriter in vb.net
Quote:
Originally Posted by
.paul.
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.
Re: StreamWriter in vb.net
Quote:
Originally Posted by
.paul.
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.
Re: StreamWriter in vb.net
Quote:
Originally Posted by
jmcilhinney
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...