.net 文件接口的封装,写日志,创建文件log

    public class FileSupport
    {
        public static FileSupport Instance = new FileSupport();
        public static string mRoot =Environment.CurrentDirectory+"/log";

        public string mPath = Environment.CurrentDirectory + "/log/" + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

        public bool FileExit()
        {
            if (!Directory.Exists(mRoot))//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(mRoot);
            }
            if (!File.Exists(mPath))
            {
               return false;
            }
            return true;
        }

        public void Write(string msg)
        {
            FileStream fs;
            if(!FileExit())
            {
               fs=   File.Create(mPath);//创建该文件
            }
            else
            {
                fs = new FileStream(mPath, FileMode.Append);
            }

            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            var addstr = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    "+msg + "\r\n";
            sw.Write(addstr);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

    }

 

posted @ 2017-12-05 10:48  leolzi  阅读(310)  评论(0编辑  收藏  举报