public static string MakeRelativePath(string fromPath, string toPath)
{
Contract.Requires(string.IsNullOrEmpty(fromPath) == false);
Contract.Requires(string.IsNullOrEmpty(toPath) == false);
Uri fromUri = new Uri(Path.GetFullPath(fromPath));
Uri toUri = new Uri(Path.GetFullPath(toPath));
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
string relativePath = Uri.UnescapeDataString(relativeUri.ToString());
if (relativePath.StartsWith("../") == false)
{
relativePath = relativePath.Substring(relativePath.IndexOf('/'));
relativePath = "." + relativePath;
}
return relativePath.Replace('/', Path.DirectorySeparatorChar);
}
}