[英]Reading from a text file using C#
So I'm making a chat program but I am having issues creating a new line in the text box instead of overwriting the other message. 因此,我正在编写一个聊天程序,但是在文本框中创建新行而不是覆盖其他消息时遇到了问题。 Here is my code:
这是我的代码:
private void refreshRate_Tick(object sender, EventArgs e)
{
String ChatPath = @"Path";
String line;
try
{
StreamReader sr = new StreamReader(@"Path");
line = sr.ReadLine();
richTextBox1.Text = null;
while (line != null)
{
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.Text = line;
line = sr.ReadLine();
}
sr.Close();
}
catch (Exception r)
{
Console.WriteLine("Exception: " + r.Message);
}
finally
{
}
}
您不需要StreamReader
或Environment.NewLine
richTextBox1.Text=File.ReadAllText(path);
If you change richTextBox1.Text = line
to richTextBox1.AppendText(line);
如果将
richTextBox1.Text = line
更改为richTextBox1.AppendText(line);
you'll lose the last line so change the while
block as: 您将丢失最后一行,因此将
while
块更改为:
while (line != null)
{
richTextBox1.AppendText(Environment.NewLine);
line = sr.ReadLine();
richTextBox1.AppendText(line??"");
}
I think you want to remove the line 我想您要删除行
richTextBox1.Text = line;
and add 并添加
richTextBox1.AppendText(line);
after you've read it from the file. 从文件中读取后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.