var pathstr="E:/Time"; try { if (Directory.Exists(pathstr)) { //var strname=DateTime.Now.ToShortDateString().Replace("/","-")+".txt"; var dt=DateTime.Now; DirectoryInfo pathinfo=new DirectoryInfo(pathstr); foreach (DirectoryInfo paths in pathinfo.GetDirectories()) { //File.AppendAllText("E:\Time\新建文件夹 (2)"+"/"+strname,DateTime.Now+" "); if (paths.CreationTime < Convert.ToDateTime(dt.AddDays(-(dt.Day)+1))) { paths.Delete(); } } } } catch (Exception ex) { File.AppendAllText("E:\Time\Errlog.txt",ex+" "); }