Delete files older than days old in a directory


ref https://stackoverflow.com/questions/2222348/delete-files-older-than-3-months-old-in-a-directory-using-net



    public static void DeleteOldFile(string dirName, int reservDay)
    {
        string[] files = Directory.GetFiles(dirName);

        foreach (string file in files)
        {
            FileInfo fi = new FileInfo(file);
            if (fi.LastAccessTime.AddDays(reservDay) < DateTime.Now)
                fi.Delete();
        }

    }

留言

熱門文章