175 lines
5.8 KiB
C#
Raw Normal View History

using System;
2022-05-13 22:41:15 +01:00
using System.IO;
using Spectre.Console;
using System.Threading;
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
{
public static int totalFiles;
2022-05-13 22:41:15 +01:00
/// <summary>
2022-05-14 02:58:38 +01:00
/// CopyDirectory will use old path and copy to new path and
2022-05-13 22:41:15 +01:00
/// asks if inner files/folders should be included
/// </summary>
/// <exception cref="DirectoryNotFoundException"></exception>
public static void CopyDirectory(string oldDir, string newDir, bool recursive)
{
AnsiConsole.Progress().Columns(
new TaskDescriptionColumn(),
new SpinnerColumn(),
new ElapsedTimeColumn()
).Start((ProgressContext context) =>
{
var dir = new DirectoryInfo(oldDir);
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (FileInfo f in dir.GetFiles())
{
totalFiles++;
}
foreach (DirectoryInfo subD in dirs)
{
foreach (FileInfo f in subD.GetFiles())
{
totalFiles++;
}
}
var task = context.AddTask("Copying files: ", true, totalFiles);
Directory.CreateDirectory(newDir);
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(newDir, file.Name);
file.CopyTo(targetFilePath, true);
}
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(newDir, subDir.Name);
AltCopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
});
//var dir = new DirectoryInfo(oldDir);
//if (!dir.Exists)
// throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
//DirectoryInfo[] dirs = dir.GetDirectories();
//Directory.CreateDirectory(newDir);
//foreach (FileInfo file in dir.GetFiles())
//{
// string targetFilePath = Path.Combine(newDir, file.Name);
// file.CopyTo(targetFilePath, true);
//}
//if (recursive)
//{
// foreach (DirectoryInfo subDir in dirs)
// {
// string newDestinationDir = Path.Combine(newDir, subDir.Name);
// CopyDirectory(subDir.FullName, newDestinationDir, true);
// }
//}
}
public static void AltCopyDirectory(string oldDir, string newDir, bool recursive)
2022-05-13 22:41:15 +01:00
{
var dir = new DirectoryInfo(oldDir);
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
DirectoryInfo[] dirs = dir.GetDirectories();
Directory.CreateDirectory(newDir);
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(newDir, file.Name);
file.CopyTo(targetFilePath, true);
}
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(newDir, subDir.Name);
AltCopyDirectory(subDir.FullName, newDestinationDir, true);
2022-05-13 22:41:15 +01:00
}
}
}
/// <summary>
2022-05-14 02:58:38 +01:00
/// DeleteFiles will use a type to look for, the path
2022-05-13 22:41:15 +01:00
/// and if all inner files/folders should be included
/// </summary>
/// <remarks>
/// Types are "file" or "folder" as a string
/// </remarks>
public static void DeleteFile(string type, string filePath, bool allFolders = false)
{
// type = "file" or "folder"
if (string.Equals(type, "file", StringComparison.OrdinalIgnoreCase))
{
File.Delete(filePath);
}
if (string.Equals(type, "folder", StringComparison.OrdinalIgnoreCase))
{
Directory.Delete(filePath, allFolders);
}
}
/// <summary>
/// finds file based on Path and File name
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <returns>String or null</returns>
public static string FindFile(string path, string name)
{
string[] filePaths = Directory.GetFiles(path);
foreach (string file in filePaths)
{
if (file.Contains(name))
{
return file;
}
}
return null;
}
/// <summary>
2022-05-14 14:21:58 +01:00
/// Finds folder with name supplied, out = directory for extracted patch folder
2022-05-13 22:41:15 +01:00
/// </summary>
/// <param name="patchRef"></param>
/// <param name="dir"></param>
2022-05-14 14:21:58 +01:00
/// <returns>bool</returns>
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;
}
}
}