c#文本文件的读取写入。当要写入的内容比较多时,同样也要使用流(Stream)的方式写入。.Net封装的类是StreamWriter。初始化StreamWriter类同样有很多方式

获取文件路径

string path = System.AppDomain.CurrentDomain.BaseDirectory + "文件配置.ini";
string path0 = Environment.CurrentDirectory.ToString();

文件是否存在

if (File.Exists(path))
{
   
       File.Delete(path);
}

创建文件写入文本文件

//创建新文件
using (FileStream fs = File.Create(path))
{
   
	//按编码编译为字节
    byte[] info = Encoding.Default.GetBytes(value);
    //字节数组写入文本件
    fs.Write(info, 0, info.Length);
}

//行写入文本文件

//向文本文件写入行
File.WriteAllLines(path, new string[] {
    "This is some text" + Environment.NewLine }, Encoding.UTF8);
 for (int i = 1; i < 100; i++)
 {
   
     File.AppendAllText(path, Convert.ToString(i, 10).ToString().ToUpper() + " ", Encoding.UTF8);
 }

//读取文件内容

using (FileStream fs = File.OpenRead(path))
 {
   
     byte[] b = new byte[256];
     UTF8Encoding temp = new UTF8Encoding(true);
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值