コード例 #1
0
        public void deleteOldReports()
        {
            DateTime currentDate = DateTime.Now;

            currentDate = currentDate.AddDays(-15);
            for (int i = 0; i < 30; i++)
            {
                currentDate = currentDate.AddDays(-1);
                String oldDirectory = Directory.GetCurrentDirectory() + "\\" + currentDate.ToString("yyyy-M-dd");
                String oldZipFile   = Directory.GetCurrentDirectory() + "\\" + currentDate.ToString("yyyy-M-dd") + ".zip";
                if (Directory.Exists(oldDirectory))
                {
                    try
                    {
                        System.IO.DirectoryInfo di = new DirectoryInfo(oldDirectory);
                        foreach (FileInfo file in di.GetFiles())
                        {
                            file.Delete();
                        }
                        foreach (DirectoryInfo dir in di.GetDirectories())
                        {
                            dir.Delete(true);
                        }
                        Directory.Delete(oldDirectory);
                        File.Delete(oldZipFile);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }