隐藏

C#定期删除日志文件

发布:2022/8/30 15:48:39作者:管理员 来源:本站 浏览次数:780

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace DelFile
{
    class Program
    {
        [STAThread]//加这个执行不弹出黑框,同时需要将项目的输出类型改为Windows应用程序,来实现无感执行
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            DeleteFile(@"E:\db_backup\", 1); //删除该目录下 超过 7天的文件
            //Console.ReadKey();
        }
        


       
        private static void DeleteFile(string fileDirect, int saveDay)
        {
            try
            {
                DateTime nowTime = DateTime.Now;
                string[] files = Directory.GetFiles(fileDirect, "*.trn", SearchOption.AllDirectories);  //获取该目录下所有 .txt文件
                foreach (string file in files)
                {
                    FileInfo fileInfo = new FileInfo(file);
                    TimeSpan t = DateTime.Now - fileInfo.CreationTime;  //当前时间  减去 文件创建时间
                    int day = t.Days;
                    if (day >= saveDay)   //保存的时间,单位:天
                    {
                        if (IsOccupy(fileInfo.FullName)) //判断文件是否被占用
                        {
                            System.IO.File.Delete(fileInfo.FullName); //删除文件
                            Console.WriteLine($"成功删除文件:{fileInfo.FullName},时间:{DateTime.Now}");
                        }
                        else
                        {
                            //Logger.Info("文件被占用,无法操作!");
                        }
                    }
                }
            }
            catch (Exception err)
            {
                throw;
            }


        }


        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);


        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);


        public const int OF_READWRITE = 2;
        public const int OF_SHARE_DENY_NONE = 0x40;
        public static readonly IntPtr HFILE_ERROR = new IntPtr(-1);


        /// <summary>
        /// 判断文件是否被占用
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private static bool IsOccupy(string file)
        {
            bool result = true; //默认状态此文件未被占用
            try
            {
                //string vFileName = @"c:\temp\temp.bmp";
                string vFileName = file;
                if (!System.IO.File.Exists(vFileName))
                {
                    //Logger.Info("文件都不存在!");
                    result = false;
                }
                IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vHandle == HFILE_ERROR)
                {
                    //Logger.Info("文件被占用!");
                    result = false;
                }
                CloseHandle(vHandle);
                //Logger.Info("没有被占用!");
            }
            catch (Exception err)
            {
                result = false;
                //Logger.Error(err);
            }
            return result;
        }


    }
}