首页 > 编程学习 > C# 删除图片,不影响程序运行

C# 删除图片,不影响程序运行

发布时间:2022/11/9 2:32:21

C# 删除图片,不影响主程序运行,使用线程操作,代码如下:

Thread detImage = new Thread(DeleteFile);
detImage.IsBackground = true;
detImage.Start();

private static void DeleteFile()
{
Param_Sys myParam_Sys = new Param_Sys();
//Stopwatch sw = Stopwatch.StartNew();
//sw.Start();
while (true)
{
//if (sw.Elapsed.Days > 1)
if(DateTime.Now.ToString(“HH:mm”) == myParam_Sys.DelImgDayTime || DateTime.Now.ToString(“HH:mm”) == myParam_Sys.DelImgNightTime)
{
Utility.WriteLog(“正在处理中,删除图片!!!!”);
try
{
DeleImageFile(“D\CenterCamera\AllImage”, myParam_Sys.Day.ToString());
}
catch (Exception ex)
{
Utility.WriteLog(“删除图片出现异常!!!”);
//MessageBox.Show(“Error:” + ex.ToString());
}
//sw.Restart();
}
}
}

private static void DeleImageFile(string path, string DaysStr)
{
try
{
if (!Directory.Exists(path)) return;
int Nums = Convert.ToInt32(DaysStr);
string[] fullnamefile = Directory.GetFileSystemEntries(path);
if (fullnamefile.Length <= Nums) return;
for (int i = 0; i < (fullnamefile.Length - Nums); i++)
{
Directory.Delete(fullnamefile[i], true);
}
}
catch (Exception ex)
{
throw;
}
}

Copyright © 2010-2022 dgrt.cn 版权所有 |关于我们| 联系方式