首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在C#的Windows窗体上显示文本文件

在C#的Windows窗体上显示文本文件
EN

Stack Overflow用户
提问于 2012-05-22 16:57:05
回答 6查看 34.6K关注 0票数 0

我正在尝试显示txt文件的内容。我想我应该使用RichTextBox来实现这个方法。我所做的是这样的。然而,它不起作用。

代码语言:javascript
复制
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();
    }
EN

Stack Overflow用户

发布于 2016-10-17 15:13:49

代码语言:javascript
复制
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);
        }
 }
票数 0
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10698799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档