首次提交:添加src文件夹代码
This commit is contained in:
79
Cowain.Bake.Communication/FTP/FTPElapsedDelete.cs
Normal file
79
Cowain.Bake.Communication/FTP/FTPElapsedDelete.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Cowain.Bake.Common;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Timers;
|
||||
using Unity;
|
||||
|
||||
namespace Cowain.Bake.Communication.FTP
|
||||
{
|
||||
//定时删除文件
|
||||
public class FTPElapsedDelete
|
||||
{
|
||||
Timer timer;
|
||||
string _deleteFilePath;
|
||||
int _intervalDays;
|
||||
public string Name { get; set; }
|
||||
public IUnityContainer _unityContainer { get; set; }
|
||||
public FTPElapsedDelete(IUnityContainer unityContainer)
|
||||
{
|
||||
_unityContainer = unityContainer;
|
||||
}
|
||||
|
||||
public void Start(string filePath, int intervalDays)
|
||||
{
|
||||
_deleteFilePath = filePath;
|
||||
_intervalDays = intervalDays;
|
||||
CreateTimer();
|
||||
}
|
||||
|
||||
void CreateTimer()
|
||||
{
|
||||
// 创建定时器并设置时间间隔为一天
|
||||
//timer = new Timer();
|
||||
timer = new Timer(TimeSpan.FromHours(12).TotalMilliseconds); //一天执行二次删除,一次就够了,怕晚上不上班
|
||||
|
||||
//timer.Interval = 10 * 1000; // 一个小时
|
||||
timer.Elapsed += TimerElapsedDeleteFiles;
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
if (timer != null)
|
||||
{
|
||||
timer.Stop();
|
||||
timer.Elapsed -= TimerElapsedDeleteFiles;
|
||||
timer.Dispose();
|
||||
timer = null;
|
||||
}
|
||||
}
|
||||
|
||||
//可配置定期删除日志文件
|
||||
void TimerElapsedDeleteFiles(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
if (Global.AppExit)
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
string[] files = Directory.GetFiles(_deleteFilePath, "*", SearchOption.AllDirectories);
|
||||
try
|
||||
{
|
||||
foreach (string file in files)
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(file); //后期有必要再多年是否上传属性,上传了就删除
|
||||
TimeSpan timeDifference = DateTime.Now - fileInfo.CreationTime;
|
||||
if (timeDifference.TotalDays >= _intervalDays)
|
||||
{
|
||||
File.SetAttributes(file, FileAttributes.Normal);
|
||||
File.Delete(file); //"对路径“”的访问被拒绝"
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"删除文件失败,_deleteFilePath:{_deleteFilePath},异常:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user