90 lines
2.8 KiB
C#
Raw Normal View History

using System;
2022-05-13 22:41:15 +01:00
using System.IO;
using Spectre.Console;
2022-05-13 22:41:15 +01:00
2022-05-14 02:58:38 +01:00
namespace SPT_AKI_Installer.Aki.Helper
2022-05-13 22:41:15 +01:00
{
public static class FileHelper
{
2022-05-19 14:41:44 +01:00
public static void CopyDirectory(string oldDir, string newDir, bool overwrite)
{
2022-05-19 14:41:44 +01:00
int totalFiles = Directory.GetFiles(oldDir, "*.*", SearchOption.AllDirectories).Length;
AnsiConsole.Progress().Columns(
2022-05-19 14:41:44 +01:00
new PercentageColumn(),
new TaskDescriptionColumn(),
2022-05-19 14:41:44 +01:00
new ProgressBarColumn(),
new ElapsedTimeColumn(),
new SpinnerColumn()
).Start((ProgressContext context) =>
{
2022-05-19 14:41:44 +01:00
var task = context.AddTask("Copying Files", true, totalFiles);
2022-05-19 14:41:44 +01:00
foreach (string dirPath in Directory.GetDirectories(oldDir, "*", SearchOption.AllDirectories))
{
2022-05-19 14:41:44 +01:00
Directory.CreateDirectory(dirPath.Replace(oldDir, newDir));
}
2022-05-19 14:41:44 +01:00
foreach (string newPath in Directory.GetFiles(oldDir, "*.*", SearchOption.AllDirectories))
{
2022-05-19 14:41:44 +01:00
File.Copy(newPath, newPath.Replace(oldDir, newDir), overwrite);
task.Increment(1);
}
});
}
2022-05-19 14:41:44 +01:00
public static void DeleteFiles(string filePath, bool allFolders = false)
2022-05-13 22:41:15 +01:00
{
2022-05-19 14:41:44 +01:00
if (filePath.Contains('.'))
2022-05-13 22:41:15 +01:00
{
2022-05-19 14:41:44 +01:00
File.Delete(filePath);
2022-05-13 22:41:15 +01:00
}
2022-05-19 14:41:44 +01:00
else
2022-05-13 22:41:15 +01:00
{
2022-05-19 14:41:44 +01:00
Directory.Delete(filePath, allFolders);
2022-05-13 22:41:15 +01:00
}
}
2022-05-19 14:41:44 +01:00
public static string FindFile(string path, string name)
2022-05-13 22:41:15 +01:00
{
2022-05-19 14:41:44 +01:00
string[] filePaths = Directory.GetFiles(path);
foreach (string file in filePaths)
2022-05-13 22:41:15 +01:00
{
2022-05-19 14:41:44 +01:00
if (file.Contains(name))
{
return file;
}
2022-05-13 22:41:15 +01:00
}
2022-05-19 14:41:44 +01:00
return null;
2022-05-13 22:41:15 +01:00
}
2022-05-19 14:41:44 +01:00
public static string FindFile(string path, string name, string altName)
2022-05-13 22:41:15 +01:00
{
string[] filePaths = Directory.GetFiles(path);
foreach (string file in filePaths)
{
2022-05-19 14:41:44 +01:00
if (file.Contains(name, StringComparison.OrdinalIgnoreCase) &&
file.Contains(altName, StringComparison.OrdinalIgnoreCase))
2022-05-13 22:41:15 +01:00
{
return file;
}
}
return null;
}
public static bool FindFolder(string patchRef, string targetPath, out DirectoryInfo dir)
2022-05-13 22:41:15 +01:00
{
var patchInfo = new FileInfo(patchRef);
var patchName = patchInfo.Name.Replace(patchInfo.Extension, "");
var path = new DirectoryInfo(Path.Join(targetPath, patchName));
2022-05-13 22:41:15 +01:00
if (path.Exists)
{
dir = path;
return true;
}
dir = null;
return false;
}
}
}