using System; using System.IO; public class Log { public Log() { logName = "" ; } public Log( string prefilename) { logName = prefilename; } private string path = System.AppDomain.CurrentDomain.BaseDirectory + "Result\\" ; public void clearTextfile() { try { if (Directory.Exists(path)) { DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] txtfiles = dir.GetFiles( "*.txt" ); foreach (FileInfo f in txtfiles) { File.Delete(f.FullName); } } } catch { } } public string logName{ get ; set ;} public void WriteLog( string msg) { try { if (!Directory.Exists(path)) Directory.CreateDirectory(path); string logfilename = path +logName+ ".txt" ; FileStream fs = null ; if (File.Exists(logfilename)) //如果文件存在就追加日志记录 { fs = new FileStream(logfilename, FileMode.Append, FileAccess.Write); } else //如果不存在,就创建日志文件 { fs = new FileStream(logfilename, FileMode.Create, FileAccess.Write); } StreamWriter sw = new StreamWriter(fs); sw.WriteLine(msg); sw.Flush(); sw.Close(); fs.Close(); } catch (Exception ex) { throw ex; } } } |
联系客服