如何在文本文件中写入所有值。请看这个问题。 [英] how to write all values in text file . please look this issue.

查看:69
本文介绍了如何在文本文件中写入所有值。请看这个问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我正在使用vb.net中的windows服务编写这些文本。



我写文本时工作正常,就像我存储a =aaa的值并在System.IO.File.WriteAllText(C:\ test1.txt,a)中编写。现在,如果你打开test1.txt文件,你可以看到aaa。



但我需要在test1.txt中写下这5个值。



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)





如果我给出如上所述,它会全部覆盖价值。所以,最后nodepad(test1.txt)只有e值(即eee)。所以,如何在test1.txt中写下所有这5个值。



i意思是如果我们打开test1.txt文件,它应该有:aaa

bbb

ccc

ddd

eee



请帮助。

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.

推荐答案

File.WriteAllText通过删除任何现有文件来创建一个新文件,并向其写入一堆文本。因此,每次调用它时,都会丢弃上次写入的数据!



有很多不同的方法可以做到这一点,但是因为它看起来像你想要在新行上使用每个字符串,请尝试以下方法:

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:
Dim lines As String() = New String() {"abc", "bbb", "ccc", "ddd", "eee"}
File.WriteAllLines("C:\text1.txt", lines)


File.WriteAllText 没有将文本附加到文件:它只是覆盖文件。尝试使用 File.WriteAllLines [ ^ ]添加所有行。

File.WriteAllText doesn't append text to the file: it just overwrites the file. Try to use File.WriteAllLines[^] to add all lines.
System.IO.File.WriteAllLines("C:\test1.txt", New String() {a, b, c, d, e})



注意:如果您只想附加一些文本,请使用 File.AppendAllText [ ^ ]或 File.AppendAllLines [ ^ ]



希望这会有所帮助。


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

Hope this helps.


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...........


这篇关于如何在文本文件中写入所有值。请看这个问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆