在Config.ini配置文件中配置文件保存的天数
public static void DeleteDir()
{
StringBuilder day = new StringBuilder();//获取配置文件中文件过期时间
GetPrivateProfileString("KeepFileDay", "day", "", day, 255,Application.StartupPath + "\\Config.ini");
stringpath = System.Environment.CurrentDirectory + "//TempFile";
if(Directory.Exists(path))
{
int KeepFileDay =Convert.ToInt32(day.ToString());
string[] DirNames =Directory.GetDirectories(path);
foreach (string s inDirNames)//遍历文件夹下所有子文件夹
{
DateTime CreateTime =Directory.GetCreationTime(s);//文件夹创建时间
DateTime TodayTime = DateTime.Today;//当日日期
TimeSpan CreateTimeTS = newTimeSpan(CreateTime.Ticks);
TimeSpan TodayTimeTS = newTimeSpan(TodayTime.Ticks);
TimeSpan TS =TodayTimeTS.Subtract(CreateTimeTS);//时间差
if (TS.Days >= KeepFileDay &&KeepFileDay != -1)//过期删除子文件夹下所有文件
{
foreach(string d in Directory.GetFileSystemEntries(s))
{
File.Delete(d);
}
Directory.Delete(s);//删除子文件夹
}
}
}
}