/ GUNRPG.Infrastructure / PathHelpers.cs
PathHelpers.cs
1 namespace GUNRPG.Infrastructure; 2 3 /// <summary> 4 /// Utility helpers for file-system path handling. 5 /// </summary> 6 public static class PathHelpers 7 { 8 /// <summary> 9 /// Returns the current user's home directory or an empty string if unavailable. 10 /// </summary> 11 public static string GetUserHomeDirectory() 12 { 13 var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); 14 return string.IsNullOrWhiteSpace(home) ? string.Empty : home; 15 } 16 17 /// <summary> 18 /// Expands a leading tilde to the user's home directory. Supports both ~/ and ~\ prefixes. 19 /// </summary> 20 public static string ExpandHomePath(string path) 21 { 22 if (string.IsNullOrWhiteSpace(path)) 23 return path; 24 25 var home = GetUserHomeDirectory(); 26 27 if (path == "~") 28 return string.IsNullOrEmpty(home) ? path : home; 29 30 if (path.StartsWith("~/", StringComparison.Ordinal) || 31 path.StartsWith("~\\", StringComparison.Ordinal)) 32 { 33 var remainder = path[2..]; 34 return string.IsNullOrEmpty(home) 35 ? remainder 36 : Path.Combine(home, remainder); 37 } 38 39 return path; 40 } 41 }