[英]Work with textBox(s) and writing in to a File and read it back from File
Can someone tell me how to; 有人可以告诉我怎么做吗? work with textBox(s) and writing the information in it to a file and read them back from the file(.txt file)
使用textBox,并将其中的信息写入文件,然后从文件(.txt文件)中读取它们
Thanks. 谢谢。
ps: I want to write some text in textbox (winforms) and when i click button save all the texts in all textboxs write to a file ps:我想在文本框(winforms)中写一些文本,当我单击按钮时,将所有文本框中的所有文本保存到文件中
Daniel 丹尼尔
That is pretty vague, but string txt = File.ReadAllText(path);
这很模糊,但是
string txt = File.ReadAllText(path);
and File.WriteAllText(path,txt);
和
File.WriteAllText(path,txt);
should handle the file part (for moderately sized files). 应该处理文件部分(适用于中等大小的文件)。
The .Text property of a TextBox contains the text within the text box. TextBox的.Text属性包含文本框中的文本。 You can get or set this property to acquire or alter the text within the TextBox as needed.
您可以获取或设置此属性以根据需要获取或更改TextBox中的文本。 Take a look at File.WriteAllText and File.ReadAllText to read/write text from/to a file.
看一下File.WriteAllText和File.ReadAllText,以从文件读取文本/向文件写入文本。
Write: 写:
FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(txtTest.Text);
sw.Close();
Read: 读:
FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
string myText = string.Empty;
while (!sr.EndOfStream)
{
myText += sr.ReadLine();
}
sr.Close();
txtTest.Text = myText;
This is what you're asking for? 这是您要的吗?
i got what i want here is the code (just for write): 我得到了我想要的是代码(仅用于写入):
public partial class Form1 : Form
{
FileProcess fileprocess;
public Form1()
{
InitializeComponent();
fileprocess = new FileProcess();
}
public void writeFile()
{
fileprocess.writeFile(textBox1.Text,textBox2.Text);
}
private void button1_Click(object sender, EventArgs e)
{
writeFile();
}
}
my class for work with the file : 我上课的文件:
class FileProcess
{
string path = @"c:\PhoneBook\PhoneBook.txt";
public void writeFile(string text1,string text2)
{
using (StreamWriter sw = new StreamWriter(path,true))
{
sw.WriteLine(text1);
sw.WriteLine(text2);
}
}
}
my mistake was that i try to store all textBox to a string like "info" and pass it through WriteFile() method and this is where i was stuck in it. 我的错误是我尝试将所有textBox存储到类似“ info”的字符串中,并通过WriteFile()方法传递它,这就是我被卡在其中的地方。
tnx to all. 所有人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.