例如:path1="c:\a"
path2="c:\a\b\c\t.txt" 相对path1的路径应该为"\b\c\t.txt"
path3="c:\a\b\c\"      相对path1的路径应该为"\b\c"怎么样能方便的获得path2和path3相对path1的相对路径呢?

解决方案 »

  1.   


    public string GetPath(string p1,string p2)
    {
      if(p1.length > p2.length)
      {
         return p1.Replace(p2,"");
      }
      else if(p1.length < p2.length)
      {
          return p2.Replace(p1,"");
      }
      else //p1==p2
      {
         return "";//or return p1;
      }
    }
      

  2.   

            public string GetPath(string p1, string p2)
            {
                int iIdx = p1.IndexOf(p2);
                int iIdy = p2.IndexOf(p1);            if (iIdx != -1 || iIdy != -1)
                {
                    if (p1.Length > p2.Length)
                    {
                        return p1.Replace(p2, "");
                    }
                    else if (p1.Length < p2.Length)
                    {
                        return p2.Replace(p1, "");
                    }
                    else //p1==p2
                    {
                        return "";//or return p1;
                    }
                }
                else
                {
                    return "";
                }        }