using System.Text; namespace Core.Helpers; /// /// Provides some path-related actions /// public static class PathHelper { /// /// Verifies the existence of a path /// /// Path to check /// If the given path does not exist public static void CheckPathValidity(string path) { if (!File.Exists(path)) throw new FileNotFoundException($"Could not found file at {path}"); } /// /// Create a file and all its parents directories if the file does not exists /// /// public static void CreateFileIfNotExists(string path) { if (!File.Exists(path)) Directory.CreateDirectory(Path.GetDirectoryName(path)!); } /// /// Extracts raw text from a file references by given path /// /// Location of file /// Raw text /// If the given path does not exist public static string TextFrom(string path) { CheckPathValidity(path); return File.ReadAllText(path); } /// /// Get the folder containing the last item of a given path /// /// /// The given path /// The folder containing the last element of the given path public static string GetFolder(string path) { path = path.Replace("\\", "/"); var s = path.Split("/").ToList(); s.RemoveAt(s.Count-1); var r = string.Join("/", s) + "/"; return r; } public static string GetName(string path) { return path.Split("/")[0]; } /// /// Concatenates a string representing the path of a folder and a string representing a file name /// The strings come from default arguments. /// /// Concatenated path public static string FullPath(params string[] pathItems) { StringBuilder builder = new StringBuilder(); foreach (var pi in pathItems) { builder.Append(pi); } return AbsolutePath(builder.ToString()); } /// /// Returns the absolute path based on a relative path /// /// relative path /// Absolute path as string public static string AbsolutePath(string relative) { return Path.GetFullPath(relative); } }