using System; using System.Collections.Generic; using System.Text; using System.IO; namespace SimpleServer { public static class Tools { static Dictionary Txt_Used = new Dictionary(); #region 生成并保存本地文件 /// /// 文件自动保存 /// /// 文件名称 /// 保存路径 /// 文件内容 public static void AddLgoToTXT(string _file_name, string path, string logstring) { //检查是否存在该路径 if (!System.IO.Directory.Exists(path)) { Directory.CreateDirectory(path); } path = path + _file_name; //检查该路径下是否存在该文件 if (!System.IO.File.Exists(path)) { FileStream stream = System.IO.File.Create(path); stream.Close(); stream.Dispose(); } if (!Txt_Used.ContainsKey(path)) { Txt_Used.Add(path, true); } else { if (Txt_Used[path]) { return; } else { Txt_Used[path] = true; } } //[1]创建文件流 文件路径 和枚举类型的文件操作类型 FileStream fs = new FileStream(path, FileMode.Append); //[2]创建写入器 StreamWriter sw = new StreamWriter(fs); //[3]以流的方式写入数据 sw.Write(logstring); //[4]关闭写入器 sw.Close(); //[5]关闭文件流 fs.Close(); Txt_Used[path] = false; } #endregion } }