C# 遍历文件夹 删除过期图片

/// 
        /// 删除过期的保存图片
        /// 
        public void DeleteOTImg(string path)
        {
            try
            {
                DirectoryInfo theFolder = new DirectoryInfo(path);
                DirectoryInfo[] dirInfo = theFolder.GetDirectories();
                DateTime dtNow = DateTime.Now;
                int saveDay = Convert.ToInt32(ConfigOperator.Single().StrSaveDays);
                //遍历文件夹
                foreach (DirectoryInfo NextFolder in dirInfo)
                {
                    //直接删除文件夹以及文件
                    /*TimeSpan ts = dtNow.Subtract(NextFolder.LastWriteTime);
                    if (ts.TotalDays > saveDay)//超过了保存时间,删除文件
                    {
                        NextFolder.Delete(true);
                    }*/
                    FileInfo[] fileInfo = NextFolder.GetFiles("*.jpg", SearchOption.AllDirectories);
                    foreach (FileInfo tmpfi in fileInfo) //遍历文件
                    {
                        TimeSpan ts = dtNow.Subtract(tmpfi.LastWriteTime);
                        if (ts.TotalDays > saveDay)//超过了保存时间,删除文件
                        {
                            tmpfi.Delete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

你可能感兴趣的:(C#)