2018-05-22 10:11:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Core.Main;
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
namespace Core.Helper
|
|
|
|
|
{
|
|
|
|
|
public static class FileHelper
|
|
|
|
|
{
|
|
|
|
|
public static void WriteTextToFile(string folderPath, string fileName, string text)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
FileHelper.WriteTextToFile(folderPath, fileName, text, Constants.confMinDate, Constants.confMinDate);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
public static void WriteTextToFile(string folderPath, string fileName, string text, DateTime creationTime, DateTime lastWriteTime)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(folderPath))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
Directory.CreateDirectory(folderPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(folderPath + fileName, text);
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
if (creationTime != Constants.confMinDate)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
File.SetCreationTimeUtc(folderPath + fileName, creationTime);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
if (lastWriteTime != Constants.confMinDate)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
File.SetLastWriteTimeUtc(folderPath + fileName, lastWriteTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
public static void CreateBackup(string filePath, string backupFolder)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
FileHelper.CreateBackup(filePath, backupFolder, "");
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
public static void CreateBackup(string filePath, string backupFolder, string backupFileName)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(backupFolder))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
Directory.CreateDirectory(backupFolder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileInfo file = new FileInfo(filePath);
|
|
|
|
|
|
|
|
|
|
string backupFilePath = backupFolder + DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss") + "_" + file.Name;
|
2018-12-03 19:45:06 +01:00
|
|
|
|
if (!backupFileName.Equals(""))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
backupFilePath = backupFolder + backupFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Copy(file.FullName, backupFilePath, true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
public static void CleanupFilesMinutes(string folderPath, int maxMinutes)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(folderPath))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
Directory.CreateDirectory(folderPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirectoryInfo folder = new DirectoryInfo(folderPath);
|
2018-12-03 19:45:06 +01:00
|
|
|
|
foreach (FileInfo file in folder.GetFiles())
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
DateTime maxAge = DateTime.Now.AddMinutes(-maxMinutes);
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
if (file.LastWriteTime < maxAge)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
File.Delete(file.FullName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
public static void CleanupFiles(string folderPath, int maxHours)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(folderPath))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
Directory.CreateDirectory(folderPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirectoryInfo folder = new DirectoryInfo(folderPath);
|
2018-12-03 19:45:06 +01:00
|
|
|
|
foreach (FileInfo file in folder.GetFiles())
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
DateTime maxAge = DateTime.Now.AddHours(-(maxHours + 1));
|
|
|
|
|
|
2018-12-03 19:45:06 +01:00
|
|
|
|
if (file.LastWriteTime < maxAge)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
File.Delete(file.FullName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|