Ejemplo n.º 1
0
        public void CleanFolder()
        {
            var folder = "C:\\Test\\ArchiveTest";
            var file1  = $"{folder}\\TestDelete1.xlsx";
            var file2  = $"{folder}\\TestDelete2.xlsx";
            var file3  = $"{folder}\\TestDelete3.xlsx";

            var daysOld = 5;

            Directory.CreateDirectory(folder);
            if (Directory.Exists(folder))
            {
                Directory.GetFiles(folder, "*.*", SearchOption.TopDirectoryOnly).ToList().ForEach(f => File.Delete(f));
            }

            var fileStream1 = File.Create(file1);
            var fileStream2 = File.Create(file2);
            var fileStream3 = File.Create(file3);

            fileStream1.Close();
            fileStream2.Close();
            fileStream3.Close();

            File.SetLastWriteTime(file1, DateTime.Now.AddDays(-3));
            File.SetLastWriteTime(file2, DateTime.Now.AddDays(-4));
            File.SetLastWriteTime(file3, DateTime.Now.AddDays(-5));

            FileUtilities.CleanFolder(folder, daysOld);

            var files = Directory.GetFiles(folder);

            Assert.AreEqual(files.First(), file1);
            Assert.AreEqual(files.Last(), file2);
            Assert.IsFalse(files.Contains(file3));
        }