diff --git a/BizHawk.MultiClient/config/NewPathConfig.cs b/BizHawk.MultiClient/config/NewPathConfig.cs index 36404849b0..9b10d0b37b 100644 --- a/BizHawk.MultiClient/config/NewPathConfig.cs +++ b/BizHawk.MultiClient/config/NewPathConfig.cs @@ -244,7 +244,7 @@ namespace BizHawk.MultiClient DialogResult result = f.ShowDialog(); if (result == DialogResult.OK) { - box.Text = f.SelectedPath; + box.Text = PathManager.TryMakeRelative(f.SelectedPath, System); } } diff --git a/BizHawk.MultiClient/config/PathManager.cs b/BizHawk.MultiClient/config/PathManager.cs index 8a04eb80bf..9caed2296a 100644 --- a/BizHawk.MultiClient/config/PathManager.cs +++ b/BizHawk.MultiClient/config/PathManager.cs @@ -363,5 +363,54 @@ namespace BizHawk.MultiClient return Path.Combine(MakeAbsolutePath(pathEntry.Path), name); } + + /// + /// Takes an absolute path and attempts to convert it to a relative, based on the system, + /// or global base if no system is supplied, if it is not a subfolder of the base, it will return the path unaltered + /// + /// + /// + /// + public static string TryMakeRelative(string absolute_path, string system = null) + { + string parent_path; + if (String.IsNullOrWhiteSpace(system)) + { + parent_path = GetBasePathAbsolute(); + } + else + { + parent_path = MakeAbsolutePath(GetPlatformBase(system)); + } + + if (IsSubfolder(parent_path, absolute_path)) + { + return absolute_path.Replace(parent_path, "."); + } + else + { + return absolute_path; + } + } + + //http://stackoverflow.com/questions/3525775/how-to-check-if-directory-1-is-a-subdirectory-of-dir2-and-vice-versa + public static bool IsSubfolder(string parentPath, string childPath) + { + var parentUri = new Uri(parentPath); + + var childUri = new DirectoryInfo(childPath).Parent; + + while (childUri != null) + { + if (new Uri(childUri.FullName) == parentUri) + { + return true; + } + + childUri = childUri.Parent; + } + + return false; + } } }