You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C#删除创建时间超过7天的年/月/日层级结构文件夹

嘿,这个需求我刚好折腾过类似的,给你整理了一套靠谱的实现方案,分步骤来哈:

核心思路

咱们的目标很明确:

  • 遍历指定根目录下的「年/月/日」层级文件夹
  • 对每个日期文件夹,判断它的创建时间是否早于7天前
  • 符合条件的就删除,同时保留最近7天的
  • 过程中要处理各种异常(比如文件夹被占用、路径不存在这类情况)
完整C#代码实现

直接上可编译的代码,你可以把它做成sample.exe:

using System;
using System.IO;

namespace FolderCleaner
{
    class Program
    {
        static void Main(string[] args)
        {
            // 配置你的根路径,比如这里改成你需要的'C:\\folder'
            string rootPath = @"C:\folder";
            // 计算7天前的日期(当前时间减去7天)
            DateTime cutoffDate = DateTime.Now.AddDays(-7);

            try
            {
                // 遍历根目录下的所有年份文件夹(比如2022、2023)
                foreach (string yearDir in Directory.GetDirectories(rootPath))
                {
                    // 尝试解析年份
                    if (!int.TryParse(Path.GetFileName(yearDir), out int year))
                    {
                        Console.WriteLine($"跳过非年份文件夹:{yearDir}");
                        continue;
                    }

                    // 遍历年份文件夹下的月份文件夹(比如09、10)
                    foreach (string monthDir in Directory.GetDirectories(yearDir))
                    {
                        if (!int.TryParse(Path.GetFileName(monthDir), out int month) || month < 1 || month > 12)
                        {
                            Console.WriteLine($"跳过非法月份文件夹:{monthDir}");
                            continue;
                        }

                        // 遍历月份文件夹下的日期文件夹(比如19、20)
                        foreach (string dayDir in Directory.GetDirectories(monthDir))
                        {
                            if (!int.TryParse(Path.GetFileName(dayDir), out int day) || day < 1 || day > DateTime.DaysInMonth(year, month))
                            {
                                Console.WriteLine($"跳过非法日期文件夹:{dayDir}");
                                continue;
                            }

                            // 获取文件夹的创建时间
                            DirectoryInfo dirInfo = new DirectoryInfo(dayDir);
                            DateTime creationTime = dirInfo.CreationTime;

                            // 判断是否超过7天
                            if (creationTime < cutoffDate)
                            {
                                try
                                {
                                    // 删除文件夹(包括里面的所有内容)
                                    dirInfo.Delete(true);
                                    Console.WriteLine($"已删除过期文件夹:{dayDir}");
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine($"删除文件夹失败 {dayDir}:{ex.Message}");
                                }
                            }
                            else
                            {
                                Console.WriteLine($"保留文件夹(7天内创建):{dayDir}");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"程序运行出错:{ex.Message}");
            }

            // 运行完可以暂停一下看结果,发布的时候可以删掉这行
            Console.WriteLine("清理完成,按任意键退出...");
            Console.ReadKey();
        }
    }
}
关键细节说明
  1. 日期判断逻辑:这里用的是文件夹的CreationTime(创建时间),完全符合你的需求;如果你的文件夹名称日期和创建时间绝对一致,也可以用文件夹名称拼接成DateTime来判断,两种方式都靠谱,看你习惯。
  2. 异常处理:每一步都加了捕获,避免单个文件夹出错导致整个程序崩溃,还会输出错误信息方便排查。
  3. 非法文件夹过滤:会跳过不是数字的年/月/日文件夹,避免误删其他无关目录。
  4. 递归删除:Delete(true)会删除文件夹里的所有内容,如果需要保留空文件夹(不过你需求是删文件夹,所以这个参数刚好)。
运行注意事项
  • 权限问题:如果你的目标文件夹在系统盘(比如C盘),可能需要右键以管理员身份运行sample.exe,否则会有权限不足的错误。
  • 测试先行:第一次运行前,可以把dirInfo.Delete(true)改成只输出日志,确认要删除的文件夹都是正确的,避免误删重要文件。
  • 定时运行:如果需要每天自动执行,可以把sample.exe加到Windows任务计划里,设置每天固定时间运行。

内容的提问来源于stack exchange,提问作者Mahsun Işık

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:27:28