
/**//// <summary>
/// 获取路径2相对于路径1的相对路径
/// </summary>
/// <param name="strPath1">路径1</param>
/// <param name="strPath2">路径2</param>
/// <returns>返回路径2相对于路径1的路径</returns>
/// <example>
/// string strPath = GetRelativePath(@"C:\WINDOWS\system32", @"C:\WINDOWS\system\*.*" );
/// //strPath == @"..\system\*.*"
/// </example>
public static string GetRelativePath(string strPath1, string strPath2)


{
if (!strPath1.EndsWith("\\" )) strPath1 += "\\";
int intIndex = -1, intPos = strPath1.IndexOf('\\');
while(intPos >= 0)

{
intPos ++;
if (string.Compare(strPath1, 0, strPath2, 0, intPos, true) != 0) break;
intIndex = intPos;
intPos = strPath1.IndexOf('\\', intPos);
}

if (intIndex >= 0)

{
strPath2 = strPath2.Substring(intIndex);
intPos = strPath1.IndexOf("\\", intIndex);
while(intPos >= 0)

{
strPath2 = "..\\" + strPath2;
intPos = strPath1.IndexOf("\\", intPos + 1);
}
}
return strPath2;
}

posted on
2006-06-02 10:18
loyee
阅读(
3641)
评论()
收藏
举报