添加引用:

using System.IO;

1.File类写入文本文件:

private void btnTextWrite_Click(object sender, EventArgs e)
{
//文件路径
string filePath = @"E:\123\456.txt"; //检测文件夹是否存在,不存在则创建
NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly)); //定义编码方式,text1.Text为文本框控件中的内容
byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
string mystr1 = Encoding.UTF8.GetString(mybyte); //写入文件
//File.WriteAllBytes(filePath,mybyte);//写入新文件
//File.WriteAllText(filePath, mystr1);//写入新文件
File.AppendAllText(filePath, mystr1);//添加至文件 }

2.File类读取文本文件:

private void btnTexRead_Click(object sender, EventArgs e)
{
//文件路径
string filePath = @"E:\123\456.txt";
try
{
if (File.Exists(filePath))
{
text1.Text = File.ReadAllText(filePath);
byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
text1.Text = Encoding.UTF8.GetString(mybyte);
}
else
{
MessageBox.Show("文件不存在");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

3.StreamWrite类写入文本文件:

private void btnTextWrite_Click(object sender, EventArgs e)
{
//文件路径
string filePath = @"E:\123\456.txt"; try
{
//检测文件夹是否存在,不存在则创建
string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly)); using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
{
byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
text1.Text = Encoding.UTF8.GetString(mybyte);
sw.Write(text1.Text);
} }
catch
{ }
}

4.StreamReader类读取文本文档:

private void btnTexRead_Click(object sender, EventArgs e)
{
//文件路径
string filePath = @"E:\123\456.txt";
try
{
if (File.Exists(filePath))
{
using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8))
{
text1.Text = sr.ReadToEnd();
byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
text1.Text = Encoding.UTF8.GetString(mybyte);
}
}
else
{
MessageBox.Show("文件不存在");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

----------------------------------------------------------------------------------------------

遇到的常见错误:

1.ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”,这在给定的上下文中无效

解决方法:Controller.File方法和System.IO.File类名称冲突的问题,只要完整输入明确类名就可解决。

例如:File.ReadAllText();  改为  System.IO.File.ReadAllText();

最新文章

  1. C#创建委托实例
  2. android webview里获取和设置cookie
  3. HIT2543 Stone IV(一定费用内的最大流)
  4. iOS工作笔记(十四)
  5. RMAN备份与恢复之删除过期备份
  6. python numpy array 的一些问题
  7. mongoDB 用java连接
  8. Leetcode 240. Search a 2D Matrix II
  9. Android Studio下添加引用jar文件和so文件
  10. git clone出现SSL错误
  11. 简单五子棋,没有电脑AI
  12. 【Unity Shaders】Lighting Models —— 衣服着色器
  13. sql server数据库入门
  14. PHP金额工具类之将阿利伯数字转换为大写中文数字
  15. Mybatis笔记三:全局配置文件
  16. Javascript的DOM总结
  17. ~/.ssh/config 配置格式
  18. Optimization algorithm----Deep Learning
  19. tp剩余未验证内容-3
  20. [QT]QStackedWidget学习使用 可用于多界面

热门文章

  1. app.module.ts说明
  2. 常见 CentOS 7 安装问题
  3. 【性能测试】:解决loadrunner执行脚本启动慢的问题
  4. springcloud(八)-Hystrix熔断器
  5. Windows开发经验 - Visual Studio 2017
  6. SVN无法读取cruuent修复方法
  7. SQL LIKE 通配符随笔
  8. C#集合通论
  9. WebDriver获得表格里所有单元格的文本
  10. Ambari集群里操作时典型权限问题put: `/home/bigdata/1.txt': No such file or directory的解决方案(图文详解)