/* * 临时文件自动存储管理 * 肖俊 * 2017-7-11 * */ using System; using System.Text; using System.Windows.Forms; namespace AllBase { public class TempFileReadWritex { private static string MainPath = Application.StartupPath + "\\temp\\"; /// /// 写入临时文件 /// /// ID /// 类型:content / desc /// 内容 public static void Write(long id, string type, string content) { string path = MainPath + type + "\\" + (id % 1024) + "\\"; FileBase.DirectoryCreate(path); string file = path + id + ".tjm"; FileBase.WriteAllText(file, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss\r\n")); FileBase.AppendAllText(file, content, Encoding.UTF8); } /// /// 读取临时文件 /// /// ID /// 类型:content / desc /// 读取成功 /// public static string Read(long id, string type, out bool hadFile) { return Read(id, type, out hadFile, BaseClass.TempSecond); } /// /// 读取临时文件 /// /// ID /// 类型:content / desc /// 读取成功 /// 过期时间,秒 /// public static string Read(long id, string type, out bool hadFile, int useTime) { hadFile = false; string path = MainPath + type + "\\" + (id % 1024) + "\\"; if (!FileBase.Exists(path)) return null; string conent = FileBase.ReadAllText(path); if (conent.Length < 21) return null; string date = conent.Substring(0, 19); DateTime saveTime; if (!DateTime.TryParse(date, out saveTime)) return null; double second = (DateTime.Now - saveTime).TotalSeconds; if (second < useTime) { hadFile = true; return conent.Substring(21); } return null; } } }