#region<写日志文件> /// <summary> /// 写日志文件,保存到指定的文件 /// </summary> /// <param name="log">日志内容</param> /// <param name="fileName">保存到文件</param> public static void WriterLog(string log, string fileName) { try { // System.Windows.Forms.MessageBox.Show(log); string logPath = System.AppDomain.CurrentDomain.BaseDirectory + @"Log" + fileName; if (System.IO.File.Exists(logPath)) CheckLogFile(logPath); Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory + @"Log"); System.IO.StreamWriter sw = new System.IO.StreamWriter(logPath, true); sw.WriteLine(DateTime.Now.ToString() + " 日志: " + log); sw.Flush(); sw.Close(); } catch { } } /// <summary> /// 读取模板文件返回文件内容 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static string ReadTemplateFile(string fileName) { try { string filePath = System.AppDomain.CurrentDomain.BaseDirectory + @"Template" + fileName; System.IO.StreamReader sr = new System.IO.StreamReader(filePath, Encoding.GetEncoding("gbk")); string str = sr.ReadToEnd(); sr.Close(); return str; } catch { return ""; } } /// <summary>de /// 如果日志文件大于1M则备份,并清空文件 /// </summary> /// <param name="filePath"></param> private static void CheckLogFile(string filePath) { try { string path = Path.GetDirectoryName(filePath) + @"aklog"; System.IO.FileInfo info = new FileInfo(filePath); if (info.Length > 1048576) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } info.CopyTo(path + @"" + DateTime.Now.Ticks.ToString() + ".txt"); info.Delete(); } } catch (Exception ex) { WriterLog(ex.Message); } } /// <summary> /// 写日志文件,保存到默认的文件:Bpfaq.log /// </summary> /// <param name="log">日志内容</param> public static void WriterLog(string log) { WriterLog(log, "ERP.log"); } #endregion<结束日志文件>