1. 程式人生 > >文件log

文件log

mpat rect director ati urn false span close 如果

    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(); } }

文件log