本文介绍: C#自动删除20天前文件夹图片

资料夹如下,需求为自动删除20天前保存的图片

如下为该方法函数,保留天数可以自定义

  public static void CleanFile()
        {
            string path = $"{SvMaster.DataPath}\Image";\文件夹路径
            DirectoryInfo dir = new DirectoryInfo(path);
            FileSystemInfo[] fsinfos = dir.GetFileSystemInfos();
            foreach (FileSystemInfo file in fsinfos)
            {
                DateTime dt = DateTime.ParseExact(file.ToString(), "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);\格式化当天日期
                if (dt < DateTime.Now.AddDays(-20))\判定保存天数
                {
                    string newPath = Path.Combine(path, file.ToString());
                    Directory.Delete(newPath, true);                   
                }
            }
        }

调用该方法:CleanFile();

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注