首页 > Windows开发 > 详细

c#有则追加,无则新建TXT内容,换行

时间:2015-12-24 13:18:41      阅读:320      评论:0      收藏:0      [点我收藏+]
using System.IO;

namespace Hydor1
{
    class CreatFile
    {
        public void creatf(string str, string name)
        {
            //获取当前日期
            string s = DateTime.Now.ToString("yyyy-MM-dd");
            string time = DateTime.Now.ToLongTimeString().ToString();
            string path = System.Windows.Forms.Application.StartupPath + @"../../../Log";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (!File.Exists(System.Windows.Forms.Application.StartupPath + @"../../../Log/" + s + ".txt"))
            {
                FileStream fs = new FileStream(System.Windows.Forms.Application.StartupPath + @"../../../Log/" + s + ".txt", FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                long fl = fs.Length;
                fs.Seek(fl, SeekOrigin.End);
                sw.WriteLine("时间        操作员姓名        操作\n");//开始写入值
                sw.WriteLine(time + "      " + name + "      " + str + "\n");//开始写入值
                sw.Close();
                fs.Close();


            }
            else
            {
                FileStream fs = new FileStream(System.Windows.Forms.Application.StartupPath + @"../../../Log/" + s + ".txt", FileMode.Open, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                long fl = fs.Length;
                fs.Seek(fl, SeekOrigin.Begin);

                sw.WriteLine(time + "      " + name + "      " + str + "\n");//开始写入值
                sw.Close();
                fs.Close();
            }
        }

    }

}

 

c#有则追加,无则新建TXT内容,换行

原文:http://www.cnblogs.com/janeaiai/p/5072590.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!