现在的位置: 首页 > 综合 > 正文

C#-计算相对路径

2011年04月22日 ⁄ 综合 ⁄ 共 1340字 ⁄ 字号 评论关闭
C#-计算相对路径

   /// <summary>
        
/// 计算相对路径
        
/// 后者相对前者的路径。
        
/// </summary>
        
/// <param name="mainDir">主目录</param>
        
/// <param name="fullFilePath">文件的绝对路径</param>
        
/// <returns>fullFilePath相对于mainDir的路径</returns>
        
/// <example>
        
/// @"..\..\regedit.exe" = GetRelativePath(@"D:\Windows\Web\Wallpaper\", @"D:\Windows\regedit.exe" );
        
/// </example>

        public static string GetRelativePath(string mainDir, string fullFilePath)
        
{
            
if (!mainDir.EndsWith("\\"))
            
{
                mainDir 
+= "\\"
            }


            
int intIndex = -1, intPos = mainDir.IndexOf('\\');
          
            
while (intPos >= 0)
            
{
                intPos
++;
                
if (string.Compare(mainDir, 0, fullFilePath, 0, intPos, true!= 0break;
                intIndex 
= intPos;
                intPos 
= mainDir.IndexOf('\\', intPos);
            }


              
if (intIndex >= 0)
            
{
                fullFilePath 
= fullFilePath.Substring(intIndex);
                intPos 
= mainDir.IndexOf("\\", intIndex);
                
while (intPos >= 0)
                
{
                    fullFilePath 
= "..\\" + fullFilePath;
                    intPos 
= mainDir.IndexOf("\\", intPos + 1);
                }

            }

           
            
return fullFilePath;
        }

抱歉!评论已关闭.