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 + " "; sw.Write(addstr); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } }