Get Relative Path - CSharp System.IO

CSharp examples for System.IO:File Path

Description

Get Relative Path

Demo Code


using System.IO;/*  ww w  .java  2 s.c om*/
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        static public string GetRelativePath(this string filespec, string folder)
        {
            Uri pathUri = new Uri(filespec);
            // Folders must end in a slash
            if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                folder += Path.DirectorySeparatorChar;
            }
            Uri folderUri = new Uri(folder);
            return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
        }
}

Related Tutorials