C# 读写txt文件方法
添加引用:
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();
C# 读写txt文件方法的更多相关文章
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- C#读写txt文件的两种方法介绍[转]
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- C#读写txt文件的两种方法介绍 v
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- Asp.net读取和写入txt文件方法(实例)!
Asp.NET读取和写入txt文件方法(实例)! [程序第一行的引入命名空间文件 - 参考] System; using System.Collections; using System.Config ...
- WPF 读写TxT文件
原文:WPF 读写TxT文件 文/嶽永鹏 WPF 中读取和写入TxT 是经常性的操作,本篇将从详细演示WPF如何读取和写入TxT文件. 首先,TxT文件希望逐行读取,并将每行读取到的数据作为一个数组的 ...
- python操作txt文件中数据教程[1]-使用python读写txt文件
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
- UNICODE环境下读写txt文件操作
内容转载自http://blog.sina.com.cn/s/blog_5d2bad130100t0x9.html UNICODE环境下读写txt文件操作 (2011-07-26 17:40:05) ...
随机推荐
- 利用python 学习数据分析 (学习三)
内容学习自: Python for Data Analysis, 2nd Edition 就是这本 纯英文学的很累,对不对取决于百度翻译了 前情提要: 各种方法贴: https://w ...
- c语言求方阵的行列式、伴随矩阵算法
#include<stdio.h> #include<math.h> #define N 100 //N比输入的阶数大即可 int main() { int n,a[N][ ...
- hive连接mysql遇到的问题
启动hive之前应该先启动metastore,也是在bin目录下:./hive --service metastore &然后再启动hive: ./hive 今天连接hive时总是报错:Una ...
- ArrayList分析
ArrayList概述 ArrayList继承了AbstractList,实现了List接口,底层基于动态数组,容量大小可以动态变化,ArrayList中可以添加null元素,另外,ArrayList ...
- jedis 连接redis
一, 单机版连接 @Test public void testJedis() { //1. 创建jedis 对象 Jedis jedis = new Jedis("192.168.88.1 ...
- win10 压缩包安装mysql8.0.11报错:Access denied for user 'root'@'localhost'
按这篇:https://blog.csdn.net/Myuhua/article/details/84792121#commentsedit 这里精简下,还有update语句中authenticati ...
- slatstack高效运维
一.简介 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. 二.诞生的背景 系统管理员日常会进行大量的重复性操作,例如安装软件, ...
- javascript的简单查询和插入和修改
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- RocketMQ学习笔记(一)eclipse版的quickstart
学而时习之,不亦说乎! 自己搭建个学习用的RocketMQ总是很麻烦,需要虚拟机环境,网络,需要安装rocketmq,启动.时间久了再去看,又不知道这个虚拟机是干嘛的了. 直接在eclipse中启动, ...
- 原创:centos7.1下 ZooKeeper 集群安装配置+Python实战范例
centos7.1下 ZooKeeper 集群安装配置+Python实战范例 下载:http://apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeepe ...