74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
/*
|
|
* 临时文件自动存储管理
|
|
* 肖俊
|
|
* 2017-7-11
|
|
*
|
|
*/
|
|
using System;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AllBase
|
|
{
|
|
public class TempFileReadWritex
|
|
{
|
|
private static string MainPath = Application.StartupPath + "\\temp\\";
|
|
/// <summary>
|
|
/// 写入临时文件
|
|
/// </summary>
|
|
/// <param name="id">ID</param>
|
|
/// <param name="type">类型:content / desc</param>
|
|
/// <param name="content">内容</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取临时文件
|
|
/// </summary>
|
|
/// <param name="id">ID</param>
|
|
/// <param name="type">类型:content / desc</param>
|
|
/// <param name="hadFile">读取成功</param>
|
|
/// <returns></returns>
|
|
public static string Read(long id, string type, out bool hadFile)
|
|
{
|
|
return Read(id, type, out hadFile, BaseClass.TempSecond);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取临时文件
|
|
/// </summary>
|
|
/// <param name="id">ID</param>
|
|
/// <param name="type">类型:content / desc</param>
|
|
/// <param name="hadFile">读取成功</param>
|
|
/// <param name="useTime">过期时间,秒</param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|