ShareX/ShareX/CleanupManager.cs

110 lines
3.7 KiB
C#
Raw Normal View History

2020-12-20 10:51:21 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2020-12-20 10:51:21 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace ShareX
{
public static class CleanupManager
{
public static void Cleanup()
2020-12-20 11:09:40 +13:00
{
2020-12-20 11:14:21 +13:00
try
{
CleanupAppTempFolder();
if (Program.Settings != null)
{
int keepFileCount = Math.Max(Program.Settings.CleanupKeepFileCount, 0);
if (Program.Settings.AutoCleanupBackupFiles)
{
CleanupFolder(SettingManager.BackupFolder, "ApplicationConfig-*.json", keepFileCount);
CleanupFolder(SettingManager.BackupFolder, "HotkeysConfig-*.json", keepFileCount);
CleanupFolder(SettingManager.BackupFolder, "UploadersConfig-*.json", keepFileCount);
CleanupFolder(SettingManager.BackupFolder, "History-*.json", keepFileCount);
}
if (Program.Settings.AutoCleanupLogFiles)
{
CleanupFolder(Program.LogsFolder, "ShareX-Log-*.txt", keepFileCount);
}
}
2020-12-20 11:14:21 +13:00
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
2020-12-20 11:09:40 +13:00
}
public static void CleanupAsync()
2020-12-20 10:51:21 +13:00
{
Task.Run(() =>
{
Cleanup();
2020-12-20 10:51:21 +13:00
});
}
private static void CleanupFolder(string folderPath, string fileNamePattern, int keepFileCount)
{
if (Directory.Exists(folderPath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
2020-12-20 10:51:21 +13:00
IEnumerable<FileInfo> files = directoryInfo.GetFiles(fileNamePattern).
OrderByDescending(f => f.LastWriteTime.Year <= 1601 ? f.CreationTime : f.LastWriteTime).Skip(keepFileCount);
2020-12-20 10:51:21 +13:00
foreach (FileInfo file in files)
{
file.Delete();
2020-12-20 11:09:40 +13:00
DebugHelper.WriteLine($"File deleted: {file.FullName}");
}
2020-12-20 10:51:21 +13:00
}
}
2020-12-20 11:09:40 +13:00
private static void CleanupAppTempFolder()
2020-12-20 10:51:21 +13:00
{
2020-12-20 11:14:21 +13:00
string tempFolder = Path.GetTempPath();
if (!string.IsNullOrEmpty(tempFolder))
2020-12-20 10:51:21 +13:00
{
2020-12-20 11:14:21 +13:00
string folderPath = Path.Combine(tempFolder, "ShareX");
2020-12-20 10:51:21 +13:00
2020-12-20 11:14:21 +13:00
if (Directory.Exists(folderPath))
2020-12-20 10:51:21 +13:00
{
2020-12-20 11:14:21 +13:00
Directory.Delete(folderPath, true);
2020-12-20 10:51:21 +13:00
2020-12-20 11:14:21 +13:00
DebugHelper.WriteLine($"ShareX temp folder cleaned: {folderPath}");
2020-12-20 10:51:21 +13:00
}
}
}
}
}