C# 文本文件与RichTextBox
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
在C#编程中,文本文件处理是常见的任务之一,它涉及到读取、写入和编辑文本数据。同时,`RichTextBox`控件是Windows Forms应用程序中用于显示和编辑富文本内容的重要工具。本文将深入探讨如何使用C#操作文本文件,并结合`RichTextBox`控件进行交互。 让我们了解如何在C#中读取和写入文本文件。最常用的方法是使用`System.IO`命名空间中的`StreamReader`和`StreamWriter`类。例如,要读取一个名为`example.txt`的文本文件,你可以这样做: ```csharp using System.IO; string filePath = "example.txt"; string content; using (StreamReader reader = new StreamReader(filePath)) { content = reader.ReadToEnd(); } ``` 这段代码会打开文件并读取其所有内容到字符串`content`中。如果要写入文本,可以使用`StreamWriter`: ```csharp string textToWrite = "这是要写入的内容"; using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine(textToWrite); } ``` 这里,我们创建了一个`StreamWriter`实例,然后使用`WriteLine`方法将文本写入文件。 `RichTextBox`控件在Windows Forms中提供了一个富文本编辑环境,支持格式化文本、颜色、字体等特性。我们可以将读取到的文本内容显示在`RichTextBox`中,或者从`RichTextBox`中保存内容到文件。例如,向`RichTextBox`添加文本: ```csharp richTextBox1.Text = content; ``` 要从`RichTextBox`中获取内容,可以使用`Rtf`属性(适用于RTF格式)或`Text`属性(适用于纯文本): ```csharp string rtfContent = richTextBox1.Rtf; string plainText = richTextBox1.Text; ``` 如果你想要将`RichTextBox`的内容保存为文本文件,可以这样操作: ```csharp string filePathToSave = "output.txt"; File.WriteAllText(filePathToSave, richTextBox1.Text); ``` 对于更复杂的操作,如读取和写入特定行,可以使用`File`类的其他方法,如`ReadLines`和`WriteAllLines`。同时,`RichTextBox`还支持事件处理,如`TextChanged`,这允许你实时监控文本变化并做出相应操作。 总结一下,C#提供了强大的文本文件处理功能,结合`RichTextBox`控件,可以轻松地实现文本的读取、写入以及富文本编辑。在实际开发中,这些基础操作常常被用于日志记录、配置文件管理、用户输入验证等各种场景。熟练掌握这些技能,对于C#开发者来说至关重要。
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cs](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
- 1
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- luoshun19904212013-04-23可以用 但不是我想要的。
![avatar](https://profile-avatar.csdnimg.cn/7b4e4def625443c294cd7d330426ae1a_a350752425.jpg!1)
- 粉丝: 54
- 资源: 2134
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)