2 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
可能最簡(jiǎn)單的方法是使用類的方法:System.IO.File
ReadAllText
myRichTextBox.Text = File.ReadAllText(filePath);
此類具有一堆靜態(tài)方法,這些方法可以為您包裝類,使讀取和寫入文件變得非常容易。StreamReader

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
有幾種方法可以讀取文件。如果你想用StreamReader來(lái)閱讀它,并想讀取整個(gè)文件,這可能是一個(gè)解決方案:
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
您可以在控制臺(tái)的輸出發(fā)生時(shí)編輯該部分。在這里,可以將RichTextbox的字符串與
text += Environment.NewLine + line;
- 2 回答
- 0 關(guān)注
- 176 瀏覽
添加回答
舉報(bào)