Tinkr2/4.2/AphelionCore/AphelionCore/Helpers/Strings.cs
/// <summary>
/// Returns a relative path from an absolute path
/// </summary>
/// <param name="baseDirectory">Base (or current) directory to use when assembling relative path</param>
/// <param name="target">Absolute path to convert</param>
/// <returns>Returns the relative path</returns>
Returns a relative path from an absolute path
Returns a relative path from an absolute path
public static string RelativePath(string baseDirectory, string target)
      {
         string sRes = string.Empty;
         int i;
         if (target.IndexOf('\\') < 0)
         {
            return target;
         }
         string[] rel1 = NormalizeDirectory(baseDirectory).Split('\\');
         string[] rel2 = target.Split('\\');
         if (rel1[1].ToLower() != rel2[1].ToLower())
         {
            return target;
         }
         int s = 0;
         int e = (rel1.Length < rel2.Length) ? rel1.Length : rel2.Length;
         for (i = 2; i < e; i++)
         {
            if (rel1[i].ToLower() != rel2[i].ToLower())
            {
               s = i;
               break;
            }
         }
         if (s < rel1.Length - 1)
         {
            for (i = s; i < rel1.Length; i++)
            {
               if (rel1[i] != string.Empty)
               {
                  sRes += "..\\";
               }
            }
         }
         if (s < rel2.Length)
         {
            for (i = s; i < rel2.Length; i++)
            {
               sRes += rel2[i] + "\\";
            }
         }
         if (Path.GetExtension(target) != string.Empty)
         {
            sRes = sRes.Substring(0, sRes.Length - 1);
         }
         return sRes;
      }
public static string RelativePath ( string baseDirectory , string target ) { string sRes = string . Empty ; int i ; if ( target . IndexOf ( ' \\ ' ) < 0 ) { return target ; } string [ ] rel1 = NormalizeDirectory ( baseDirectory ) . Split ( ' \\ ' ) ; string [ ] rel2 = target . Split ( ' \\ ' ) ; if ( rel1 [ 1 ] . ToLower ( ) != rel2 [ 1 ] . ToLower ( ) ) { return target ; } int s = 0 ; int e = ( rel1 . Length < rel2 . Length ) ? rel1 . Length : rel2 . Length ; for ( i = 2 ; i < e ; i ++ ) { if ( rel1 [ i ] . ToLower ( ) != rel2 [ i ] . ToLower ( ) ) { s = i ; break ; } } if ( s < rel1 . Length - 1 ) { for ( i = s ; i < rel1 . Length ; i ++ ) { if ( rel1 [ i ] != string . Empty ) { sRes += " \\ " ; } } } if ( s < rel2 . Length ) { for ( i = s ; i < rel2 . Length ; i ++ ) { sRes += rel2 [ i ] + " \\ " ; } } if ( Path . GetExtension ( target ) != string . Empty ) { sRes = sRes . Substring ( 0 , sRes . Length - 1 ) ; } return sRes ; }
Returns a relative path from an absolute path
Returns a relative path from an absolute path
// Check for file only // Split strings // Check for different drive // Find last match // Build up-line // Build down-line // Check for file // Return outcome
Returns the relative path
Returns the relative path
baseDirectory
string
Base (or current) directory to use when assembling relative path
Base ( or current ) directory to use when assembling relative path
target
string
Absolute path to convert
Absolute path to convert