2022-05-14 12:19:40 +01:00
|
|
|
|
using System;
|
2022-05-13 22:41:15 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
/// Finds folder with name supplied
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="patchRef"></param>
|
|
|
|
|
/// <param name="dir"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-05-14 12:19:40 +01:00
|
|
|
|
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, "");
|
2022-05-14 12:19:40 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|