using System;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "targetFolder");
string[] files = Directory.GetFiles(folderPath);
DateTime now = DateTime.Now.AddDays(-3); //3일 전 파일까지 검출용.
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
//A < B = 1, A == B = 0, A > B = -1
if (DateTime.Compare(now, fileInfo.LastWriteTime) > 0) //기준보다 오래된 파일 삭제
{
fileInfo.Delete();
}
}
}
}
}
Process.Start 혹은 Process 클래스를 구현하여 사용하여 프로그램 실행이 가능합니다.
아래는 엑셀, 텍스트 문서를 열은 예제입니다.
using System;
using System.Diagnostics;
using System.IO;
namespace ConsoleApp
{
class Program
{
public static void Main(string[] args)
{
Process process = new Process();
/* 프로그램 실행 시키고 기다리기.
Process pro = Process.Start("...");
pro.WaitForExit();
*/
Console.WriteLine("[Excel] 프로세스 시작!");
process.StartInfo.Arguments = Path.Combine(Directory.GetCurrentDirectory(), "test.xlsx");
process.StartInfo.FileName = "excel.exe";
process.StartInfo.UseShellExecute = true; //엑셀 실행 시 UseShellExecute 필요!
process.Start();
process.WaitForExit();
//위와 동일한 작업을 합니다!
//Process pro = Process.Start(new ProcessStartInfo("excel.exe", Path.Combine(Directory.GetCurrentDirectory(), "test.xlsx")) { UseShellExecute = true });
//pro.WaitForExit();
Console.WriteLine("[Excel] 프로세스 종료!");
Console.WriteLine("[notepad] 프로세스 시작!");
process.StartInfo.Arguments = Path.Combine(Directory.GetCurrentDirectory(), "test.txt");
process.StartInfo.FileName = "notepad.exe";
process.Start();
process.WaitForExit();
Console.WriteLine("[notepad] 프로세스 종료!");
}
}
}