Delete files older than 3 months old in a directory using .NET

前端 未结 18 2105
醉话见心
醉话见心 2020-12-02 06:31

I would like to know (using C#) how I can delete files in a certain directory older than 3 months, but I guess the date period could be flexible.

Just to be clear:

18条回答
  •  醉话见心
    2020-12-02 06:58

    For example: To go My folder project on source, i need to up two folder. I make this algorim to 2 days week and into four hour

    public static void LimpiarArchivosViejos()
        {
            DayOfWeek today = DateTime.Today.DayOfWeek;
            int hora = DateTime.Now.Hour;
            if(today == DayOfWeek.Monday || today == DayOfWeek.Tuesday && hora < 12 && hora > 8)
            {
                CleanPdfOlds();
                CleanExcelsOlds();
            }
    
        }
        private static void CleanPdfOlds(){
            string[] files = Directory.GetFiles("../../Users/Maxi/Source/Repos/13-12-2017_config_pdfListados/ApplicaAccWeb/Uploads/Reports");
            foreach (string file in files)
            {
                FileInfo fi = new FileInfo(file);
                if (fi.CreationTime < DateTime.Now.AddDays(-7))
                    fi.Delete();
            }
        }
        private static void CleanExcelsOlds()
        {
            string[] files2 = Directory.GetFiles("../../Users/Maxi/Source/Repos/13-12-2017_config_pdfListados/ApplicaAccWeb/Uploads/Excels");
            foreach (string file in files2)
            {
                FileInfo fi = new FileInfo(file);
                if (fi.CreationTime < DateTime.Now.AddDays(-7))
                    fi.Delete();
            }
        }
    

提交回复
热议问题