C#写入文本文档
2021-07-19 23:17
标签:lin exist threading 文件 内容 file ffffff collect ring C#写入文本文档 标签:lin exist threading 文件 内容 file ffffff collect ring 原文地址:http://www.cnblogs.com/programsky/p/8047566.htmlusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Log
{
class Program
{
static void Main(string[] args)
{
if (File.Exists(@"D:\test.txt"))
{
//存在,读取之前的内容并写入现在的内容
StreamWriter sw = new StreamWriter(@"d:\test.txt", true, Encoding.GetEncoding("gb2312"));
for (int i = 0; i 5; i++)
{
sw.WriteLine("当前时间:"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff"));
}
sw.Close();//关闭流
}
else
{
//不存在,写入文本文件
File.WriteAllText(@"d:\test.txt","", Encoding.GetEncoding("gb2312"));
}
}
}
}