C# 如何在c中检查工作日#

C# 如何在c中检查工作日#,c#,.net,.net-3.5,C#,.net,.net 3.5,我的客户昨天给了我非常不同的要求 我在服务器上有一个文件夹,每天有数千个文件提交。他要我写一个逻辑来检查文件的日期。如果文件夹中有超过3个工作日(周一至周五)的文件,则他希望我删除这些文件 示例:如果星期六在文件夹中创建了任何文件,则该文件应在星期三删除,因为在星期六和星期日之间,不应算作工作日 我的开发环境是c#NET 3.5 我想我应该编写自定义方法 请帮帮我 使用枚举所有文件 使用获取文件的创建日期 获取该日期与今天之间的工作日: 和 编写LINQ查询以确定是否满足条件 var Fil

我的客户昨天给了我非常不同的要求

我在服务器上有一个文件夹,每天有数千个文件提交。他要我写一个逻辑来检查文件的日期。如果文件夹中有超过3个工作日(周一至周五)的文件,则他希望我删除这些文件

示例:如果星期六在文件夹中创建了任何文件,则该文件应在星期三删除,因为在星期六和星期日之间,不应算作工作日

我的开发环境是c#NET 3.5

我想我应该编写自定义方法

请帮帮我

  • 使用枚举所有文件

  • 使用获取文件的创建日期

  • 获取该日期与今天之间的工作日:

  • 编写LINQ查询以确定是否满足条件

    var FilesToDelete =
        from filePath in Directory.GetFiles("folder")
        where File.GetCreationTime(filePath).BusinessDaysUntil(DateTime.Today) > 3
        select filePath;
    

  • 请记住将扩展方法放在静态类中。

    如果您只需要文件创建日期,我不会使用.NET类,如果有数千个文件需要检查,它们会产生大量开销

    由于我无法在一个目录(50k+)中处理大量文件,而且需要性能,因此我会使用
    FindFirstFile
    FindNextFile
    FindClose
    ,以实现更快的处理速度和更少的开销

    WIN32_FIND_DATA findData;
    
    DateTime.FromFileTimeUtc((长)findData.ftLastWriteTime.dwLowDateTime+
    
    (long)findData.ftLastWriteTime.dwHighDateTime*4294967296

    乔治·达克特的解决方案正适合您。
    为了帮助您,我发布了一个示例:

    public static class DateExtensions
    {
        public static bool IsBusinessDay(this DateTime date)
        {
            return
                date.DayOfWeek != DayOfWeek.Saturday &&
                date.DayOfWeek != DayOfWeek.Sunday;
        }
        public static int BusinessDaysTo(this DateTime fromDate, DateTime toDate, 
                                         int maxAllowed = 0)
        {
            int ret = 0;
            DateTime dt = fromDate;
            while (dt < toDate)
            {
                if (dt.IsBusinessDay()) ret++;
                if (maxAllowed > 0 && ret == maxAllowed) return ret;
                dt = dt.AddDays(1);
            }
            return ret;
        }
    }
    
    只要调用:

    var cleaner = new FileCleaner();
    clenaer.DeleteOldFiles(@"C:\YourDiretory");
    
    代码

    公共类文件清理器
    {
    公共IEnumerable GetOldFiles(字符串目录名)
    {
    var limit=DateTime.Now.AddBusinessDays(-3);
    返回Directory.GetFiles(directoryName)
    .Where(file=>file.GetCreationTime(file)0)
    {
    newDate=newDate.AddDays(-1);
    if(newDate.IsBusinessDay())
    --天;
    }
    返回newDate;
    }
    }
    
    你也介意假日吗?我的意思是,圣诞节、复活节和地区假日?不,他们希望假日作为工作日。我会使用
    文件.GetCreationTime
    而不是为每个文件创建一个新的
    文件信息
    。这并不能回答问题。根据你的经验,上面的代码比使用.NET类快多少?@MEMark I好久没有确切的数字了。根据文件的数量,数字从1到5不等。
    WIN32_FIND_DATA findData;
    
    public static class DateExtensions
    {
        public static bool IsBusinessDay(this DateTime date)
        {
            return
                date.DayOfWeek != DayOfWeek.Saturday &&
                date.DayOfWeek != DayOfWeek.Sunday;
        }
        public static int BusinessDaysTo(this DateTime fromDate, DateTime toDate, 
                                         int maxAllowed = 0)
        {
            int ret = 0;
            DateTime dt = fromDate;
            while (dt < toDate)
            {
                if (dt.IsBusinessDay()) ret++;
                if (maxAllowed > 0 && ret == maxAllowed) return ret;
                dt = dt.AddDays(1);
            }
            return ret;
        }
    }
    
    DateTime from = DateTime.Now.AddDays(-8);
    int ret = from.BusinessDaysTo(DateTime.Now);
    int ret2 = from.BusinessDaysTo(DateTime.Now.AddDays(5), 8);
    
    var cleaner = new FileCleaner();
    clenaer.DeleteOldFiles(@"C:\YourDiretory");
    
    public class FileCleaner
    {
        public IEnumerable<string> GetOldFiles(string directoryName)
        {
            var limit = DateTime.Now.AddBusinessDays(-3);
            return Directory.GetFiles(directoryName)
                            .Where(file => File.GetCreationTime(file) < limit);
        }
    
        public void DeleteOldFiles(string directoryName)
        {
            foreach (var filename in GetOldFiles(directoryName))
            {
                File.Delete(filename);
            }
        }
    }
    
    
    public static class DateTimeExtensions
    {
        public static bool IsBusinessDay(this DateTime instance)
        {
            return instance.DayOfWeek != DayOfWeek.Saturday 
                   && instance.DayOfWeek != DayOfWeek.Sunday;
        }
        public static DateTime AddBusinessDays(this DateTime instance, int days)
        {
            var newDate = instance;
            while (days > 0)
            {
                newDate = newDate.AddDays(-1);
                if (newDate.IsBusinessDay())
                    --days;
            }
            return newDate;
        }
    }