void Start()
{
//DeleteOldImages("E:\\AerialImages\\2023_08_24_15_57");
DeleteDirectory("E:\\AerialImages\\2023_08_24_15_57");
}
/// <summary>
/// 删除指定文件夹下文件
/// </summary>
/// <param name="fullDirPath"></param>
private void DeleteOldImages(string fullDirPath)
{
//删除JPG图像,也可以删除其他文件类型
string[] files = Directory.GetFiles(fullDirPath, "*", SearchOption.AllDirectories);
List<string> oldImages = new List<string>();
for (int i = 0; i < files.Length; i++)
{
string[] fileNames = files[i].Split("\\");
if (fileNames[fileNames.Length - 1].ToUpper().EndsWith(".JPG"))
oldImages.Add(files[i]);
}
if (oldImages.Count > 0)
{
for (int i = oldImages.Count - 1; i >= 0; i--)
{
if (File.Exists(oldImages[i]))
{
Debug.LogError($"删除文件: {oldImages[i]}");
File.Delete(oldImages[i]);
}
}
}
files = null;
oldImages.Clear();
}
/// <summary>
/// 删除指定文件夹
/// </summary>
/// <param name="dirPath"></param>
private void DeleteDirectory(string dirPath)
{
if (Directory.Exists(dirPath))
Directory.Delete(dirPath,true);//true 该文件夹下所有内容都会被删除
}