Nexus.Client.Util.FileUtil.RelativizePath C# (CSharp) Method

RelativizePath() public static method

Generates a path that is relative to another path.
Thrown if either parameter is null.
public static RelativizePath ( string p_strRoot, string p_strPath ) : string
p_strRoot string The root directory with respect to which the path will be made relative.
p_strPath string The path to make relative.
return string
		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());
		}