Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi i am writing these text using windows service in vb.net.

It works fine when i write text like i am storing value for a="aaa" and writing in System.IO.File.WriteAllText("C:\test1.txt",a) . now if you open the test1.txt file you can see aaa.

but i need to write these 5 value in test1.txt.

a="abc"
b="bbb"
c="ccc"
d="ddd"
e="eee"

System.IO.File.WriteAllText("C:\test1.txt", a)
System.IO.File.WriteAllText("C:\test1.txt", b)
System.IO.File.WriteAllText("C:\test1.txt", c)
System.IO.File.WriteAllText("C:\test1.txt", d)
System.IO.File.WriteAllText("C:\test1.txt", e)


if i give like above, it ovewrites the all value . so, finally the nodepad(test1.txt) has e value only (ie, eee). so, how to write all these 5 value in test1.txt.

i mean if we open test1.txt file, it shoud have: aaa
bbb
ccc
ddd
eee

Please help.
Posted

File.WriteAllText creates a new file by deleting any existing file, and writes a bunch of text to it. So, each time you call it, you scrap the data you wrote last time!

There are a lot of different ways to do this, but since it seems like you want each string on a new line, try this:
VB
Dim lines As String() = New String() {"abc", "bbb", "ccc", "ddd", "eee"}
File.WriteAllLines("C:\text1.txt", lines)
 
Share this answer
 
Comments
Thomas Daniels 16-Oct-13 11:52am    
You beat it to me! +5!
OriginalGriff 16-Oct-13 12:02pm    
Luckily for both of us, I'm not that way inclined! :laugh:
File.WriteAllText doesn't append text to the file: it just overwrites the file. Try to use File.WriteAllLines[^] to add all lines.
VB
System.IO.File.WriteAllLines("C:\test1.txt", New String() {a, b, c, d, e})

Note: if you just want to append some text, then use File.AppendAllText[^] or File.AppendAllLines[^]

Hope this helps.
 
Share this answer
 
v2
Comments
christhuxavier 16-Oct-13 12:16pm    
I got it. Thanks a lot!.
Thomas Daniels 16-Oct-13 12:17pm    
You're welcome!
Sergey Alexandrovich Kryukov 16-Oct-13 15:52pm    
5ed, just for showing the useful alternative.
—SA
Thomas Daniels 18-Oct-13 12:19pm    
Thank you!
VB
Dim sb As StringBuilder = New StringBuilder()
        sb.AppendLine("This is the first line")
        sb.AppendLine("This is the second line")
        sb.AppendLine("This is the third line")
        ' Just one call to IO subsystem
        File.AppendAllText("d:\tes1.text", sb.ToString())



it will work i tried it
and also give out put which you required.

please give me reply what happen it work or not...........
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900