using System; using System.IO; using System.Reflection; namespace RGB.NET.Core { /// /// Offers some helper-methods for file-path related things. /// public static class PathHelper { #region Events /// /// Occurs when a path is resolving. /// public static event EventHandler ResolvingAbsolutePath; #endregion #region Methods /// /// Returns an absolute path created from an relative path relatvie to the location of the executung assembly. /// /// The relative part of the path to convert. /// The absolute path. public static string GetAbsolutePath(string relativePath) => GetAbsolutePath((object)null, relativePath); /// /// Returns an absolute path created from an relative path relatvie to the location of the executung assembly. /// /// The relative part of the path to convert. /// The file name of the path to convert. /// The absolute path. public static string GetAbsolutePath(string relativePath, string fileName) => GetAbsolutePath(null, relativePath, fileName); /// /// Returns an absolute path created from an relative path relatvie to the location of the executung assembly. /// /// The requester of this path. (Used for better control when using the event to override this behavior.) /// The relative path to convert. /// The file name of the path to convert. /// The absolute path. public static string GetAbsolutePath(object sender, string relativePath, string fileName) { string relativePart = Path.Combine(relativePath, fileName); string assemblyLocation = Assembly.GetEntryAssembly()?.Location; if (assemblyLocation == null) return relativePart; string directoryName = Path.GetDirectoryName(assemblyLocation); string path = directoryName == null ? null : Path.Combine(directoryName, relativePart); ResolvePathEventArgs args = new ResolvePathEventArgs(relativePath, fileName, path); ResolvingAbsolutePath?.Invoke(sender, args); return args.FinalPath; } /// /// Returns an absolute path created from an relative path relatvie to the location of the executung assembly. /// /// The requester of this path. (Used for better control when using the event to override this behavior.) /// The relative path to convert. /// The absolute path. public static string GetAbsolutePath(object sender, string relativePath) { string assemblyLocation = Assembly.GetEntryAssembly()?.Location; if (assemblyLocation == null) return relativePath; string directoryName = Path.GetDirectoryName(assemblyLocation); string path = directoryName == null ? null : Path.Combine(directoryName, relativePath); ResolvePathEventArgs args = new ResolvePathEventArgs(relativePath, path); ResolvingAbsolutePath?.Invoke(sender, args); return args.FinalPath; } #endregion } }