C#/c#
[ c# ] 매일 밤 12시 폴더 생성 , 폴더 삭제 Task 이용
code094
2022. 12. 28. 09:16
폴더 생성 태스크
while (true)
현재 요일 폴더 있는지 확인함
없으면 생성
현재 시간과 오늘 오후 11:59 시간 차이 계산
시간 차이만큼 sleep
폴더 제거 태스크
while (true)
설정된 시간 (한 달)보다 더 오래된 폴더 제거
현재 시간과 다음 날 0:00 시간 차이 계산
시간 차이만큼 sleep
try
{
string firstHttpFolderPath = "." + "/" + DateTime.Now.ToString("yyyyMMdd");
DirectoryInfo firstDi = new DirectoryInfo(firstHttpFolderPath);
if (firstDi.Exists == false)
{
firstDi.Create();
}
Task.Factory.StartNew(() =>
{
while (true)
{
string httpFolderPath = "." + "/" + DateTime.Now.AddDays(1).ToString("yyyyMMdd");
DirectoryInfo di = new DirectoryInfo(httpFolderPath);
if (di.Exists == false)
{
di.Create();
}
string currentTime = DateTime.Now.ToString("HHmmss");
DateTime compareTime = DateTime.Now.AddDays(1).Date.AddMinutes(-1);
int sleepTime = Convert.ToInt32((compareTime - DateTime.Now).TotalSeconds);
Thread.Sleep(sleepTime);
}
});
Task.Factory.StartNew(() =>
{
while (true)
{
string httpFolderPath = ".";
DirectoryInfo di = new DirectoryInfo(httpFolderPath);
DateTime deleteDate = DateTime.Now.AddMonths(-1);
var folder = di.GetDirectories(httpFolderPath);
foreach (var folderName in folder)
{
DateTime compareName = DateTime.ParseExact(folderName.Name, "yyyyMMdd", provider);
if (compareName < deleteDate)
folderName.Delete();
}
string currentTime = DateTime.Now.ToString("HHmmss");
DateTime nextDay = DateTime.Now.AddDays(1).Date;
nextDay.AddDays(1);
int sleepTime = Convert.ToInt32((nextDay - DateTime.Now).TotalSeconds);
//Thread.Sleep(sleepTime);
}
});
}
catch (Exception ex)
{
log.Error(ex.Message);
}