清理过期日志

        public static void DelOverdueFiles(int days = 1)
        {
            string logDir = AppDomain.CurrentDomain.BaseDirectory + "/Logs/";
            if (!Directory.Exists(logDir))
            {
                return;
            }
            DirectoryInfo TheFolder = new DirectoryInfo(logDir);
            FileInfo[] files = TheFolder.GetFiles("*.txt", SearchOption.AllDirectories);
            foreach (FileInfo f2 in files.Where((FileInfo f) => (DateTime.Now - f.CreationTime).TotalDays > (double)days))
            {
                try
                {
                    File.Delete(f2.FullName);
                }
                catch (Exception)
                {
                }
            }
        }

 

posted @ 2023-06-09 13:29  simadi  阅读(13)  评论(0编辑  收藏  举报