C # implementation creates a log file every day

First, write the log file class

/*
 * 主题:日志文件
 * 
 * 功能:
 *      1、每天创建日志文件
 *      2、可以向日志文件追加内容,不被覆盖
 *      3、可以读取日志文件的内容
 *      4、可以清空日志文件内容
 *      5、删除指定的日志文件
 *      
 * 作者:Coffee
 * 
 */

using System;
using System.IO;
using System.Text;

namespace Kernal
{
    class EveryDayLog
    {
        #region   当前文件输出路径
        public static string SYSTEM_OUTLOG_FILEDIRECTORY = Directory.GetCurrentDirectory() + "/EveryDayLog/";

        #endregion



        #region   公有方法

        /// <summary>
        /// 给日志文件写入信息
        /// </summary>
        /// <param name="info">需要写入的信息</param>
        /// <returns>true:表示写入成功</returns>
        public static bool Write(string needWriteInfo)
        {
            bool success = false;
            if (File.Exists(LogPath))
            {
                //追加文件
                using (FileStream fs = new FileStream(LogPath, FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                    {
                        //开始写入
                        sw.WriteLine(DateTime.Now + "   " + needWriteInfo);
                        success = true;
                    }
                }
            }
            else
            {
                using (FileStream fs = new FileStream(LogPath, FileMode.CreateNew, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                    {
                        //开始写入
                        sw.WriteLine(DateTime.Now + ": " + needWriteInfo);
                        success = true;
                    }
                }
            }

            return success;
        }

        /// <summary>
        /// 读取日志文件的信息
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>返回读取的信息</returns>
        public static string Read(string path)
        {
            string contents = string.Empty;
            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                    {
                        //开始读取
                        contents = sr.ReadToEnd();
                    }
                }
            }
            return contents;
        }

        /// <summary>
        /// 清除日志文件的信息
        /// </summary>
        /// <param name="path">文件的路径</param>
        /// <returns>true:表示清空成功</returns>
        public static bool Clear(string path)
        {
            bool success = false;
            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    fs.Seek(0,SeekOrigin.Begin);
                    fs.SetLength(0);
                    success = true;
                }
            }
            return success;
        }

        /// <summary>
        /// 删除日志文件
        /// </summary>
        /// <param name="path">文件的路径</param>
        /// <returns>true:表示删除成功</returns>
        public static bool Delete(string path)
        {
            bool success = false;
            if (File.Exists(path))
            {
                File.Delete(path);
                success = true;
            }
            return success;
        }

        #endregion

        #region   私有方法

        /// <summary>
        /// 日志文件存放路径
        /// </summary>
        private static string LogPath
        {
            get
            {
                string OutPath = SYSTEM_OUTLOG_FILEDIRECTORY;

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

                return OutPath + DateTime.Today.ToString("yyyy-MM-dd") + ".txt";
            }
        }

        #endregion

    }//Class_end
}

Second, the test

 

using Kernal;
using System;

namespace XmlHelper
{
    class Program
    {
        static void Main(string[] args)
        {


            for (int i = 0; i <= 100; i++)
            {
                EveryDayLog.Write("这是一个测试!!!!" + i);

                if (i == 100)
                {
                    Console.WriteLine("写入完成!!!");
                }
            }

            string path = @"C:\Users\CoffeeMilk\Desktop\XmlHelper\XmlHelper\bin\Debug\EveryDayLog\2019-05-27.txt";

            //读取文件内容
            string a = EveryDayLog.Read(path);
            Console.WriteLine(a);

            //清空文件内容
            bool success = EveryDayLog.Clear(path);
            Console.WriteLine("清空情况=" + success);

            ////删除文件
            //bool success1 = EveryDayLog.Delete(path);
            //Console.WriteLine("删除情况=" + success1);

            Console.Read();

        }//Class_end


    }
}

Third, the effect

 

 

 Reference: C # string to write to a file, or appending the string file

            C # emptied the contents of txt, and additional content

 

Guess you like

Origin blog.csdn.net/xiaochenXIHUA/article/details/90610170