我正在尝试显示txt文件的内容。我想我应该使用RichTextBox来实现这个方法。我所做的是这样的。然而,它不起作用。
public static byte[] ReadFile() {
FileStream fileStream = new FileStream(@"help.txt", FileMode.Open, FileAccess.Read);
byte[] buffer;
try {
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
} finally {
fileStream.Close();
}
return buffer;
}
private void richTextBox1_TextChanged(object sender, EventArgs e) {
ReadFile();
}发布于 2016-10-17 15:13:49
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
label1.Text = openFileDialog1.FileName;
richTextBox1.Text = File.ReadAllText(label1.Text);
}
}https://stackoverflow.com/questions/10698799
复制相似问题