如何替换文件的内容,然后保存新文件 [英] how to replace the content of a file and then save the new file

查看:87
本文介绍了如何替换文件的内容,然后保存新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜。我正在创建一个胜利形式。在我的形式,我已添加do while循环..我已添加TABLE LAYOUT PANEL ..在此,文本框被声明,它在布局面板内创建文本框..但我的问题是我可以读取文件,但如果我想要更改文本框中写入的文本,我无法保存文件..我想替换文件的旧内容然后覆盖它..plz帮助我

hi. i am creating a win form. in my form i have added do while loop.. i have added TABLE LAYOUT PANEL ..in this, the text box is declared and it creates text boxes inside the layout panel.. but my problem is i can read file but if i want to change the text written in text box, i am not able to save the file.. i want to replace the old content of a file and then overwrite it..plz help me

推荐答案

我假设你做的事情就像从文件中读取每一行到一个单独的TextBox?

如果是这样,那么你需要循环遍历面板中的所有Textboxes(你可以使用它的Controls集合可以访问它们并将每一行写回一个单独的文件。



但问题是:你为什么这样做?为什么不将一个TextBox设置为 MultiLine = true 并将其用于整个文件?然后它只是一个案例:

I assume that you do something like read each line from the file into a separate TextBox?
If so, then you need to loop through all the Textboxes in the panel (you can use its Controls collection to access them) and write each line back to a separate file.

But the question is: why are you doing that? Why not just set a single TextBox to MultiLine=true and use that for the whole file? Then it is just a case of:
myTextBox.Lines = File.ReadAllLines(path)



And

File.WriteAllLines(path, myTextBox.Lines)


这篇关于如何替换文件的内容,然后保存新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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