-
Nov 21st, 2021, 06:05 PM
#1
Thread Starter
Junior Member
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()
-
Nov 21st, 2021, 07:08 PM
#2
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.
-
Nov 21st, 2021, 07:22 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 21st, 2021, 10:31 PM
#4
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..
-
Nov 21st, 2021, 10:44 PM
#5
Re: StreamWriter in vb.net
@jm - Your code also assumes that the user is using >=VS2012
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 21st, 2021, 11:54 PM
#6
Re: StreamWriter in vb.net
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.
-
Nov 22nd, 2021, 12:03 AM
#7
Re: StreamWriter in vb.net
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 22nd, 2021, 02:51 AM
#8
Re: StreamWriter in vb.net
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.
-
Nov 22nd, 2021, 02:54 AM
#9
Re: StreamWriter in vb.net
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.
-
Nov 22nd, 2021, 02:58 AM
#10
Re: StreamWriter in vb.net
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|