public static string RelativizePath(string p_strRoot, string p_strPath)
{
if (p_strRoot == null)
throw new ArgumentNullException("p_strFrom");
if (p_strPath == null)
throw new ArgumentNullException("p_strRoot");
p_strRoot = p_strRoot.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
p_strPath = p_strPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (Path.IsPathRooted(p_strRoot) && Path.IsPathRooted(p_strPath) && !Path.GetPathRoot(p_strRoot).Equals(Path.GetPathRoot(p_strPath), StringComparison.OrdinalIgnoreCase))
return p_strPath;
string[] strRootPaths = p_strRoot.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
string[] strPathsToRelativize = p_strPath.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
Int32 intMinPathLength = Math.Min(strRootPaths.Length, strPathsToRelativize.Length);
Int32 intLastCommonPathIndex = -1;
for (Int32 i = 0; i < intMinPathLength; i++)
{
if (!strRootPaths[i].Equals(strPathsToRelativize[i], StringComparison.OrdinalIgnoreCase))
break;
intLastCommonPathIndex = i;
}
if (intLastCommonPathIndex == -1)
return p_strPath;
List<string> lstRelativePaths = new List<string>();
for (Int32 i = intLastCommonPathIndex + 1; i < strRootPaths.Length; i++)
lstRelativePaths.Add("..");
for (Int32 i = intLastCommonPathIndex + 1; i < strPathsToRelativize.Length; i++)
lstRelativePaths.Add(strPathsToRelativize[i]);
return String.Join(Path.DirectorySeparatorChar.ToString(), lstRelativePaths.ToArray());
}