C#写入文本文档

using 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"));
            }
        }
    }
}

 

posted @ 2017-12-16 20:32  夏空  阅读(1076)  评论(0编辑  收藏  举报