c#如何读取文件的内容
时间: 2023-12-08 14:38:55 浏览: 117
以下是两种C#读取文件内容的方法:
1.使用FileStream和Encoding类读取文件内容:
```csharp
this.rtb_Content.Clear();
FileStream fs = new FileStream(this.txt_FilePath.Text, FileMode.Open, FileAccess.Read);
int n = (int)fs.Length;
byte[] b = new byte[n];
int r = fs.Read(b, 0, n);
fs.Close();
this.rtb_Content.Text = Encoding.UTF8.GetString(b, 0, n);
```
2.使用FileStream和StreamReader类读取文件内容:
```csharp
this.rtb_Content.Clear();
FileStream fs = new FileStream(this.txt_FilePath.Text, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
this.rtb_Content.Text = sr.ReadToEnd();
fs.Close();
sr.Close();
```
阅读全文