Blog

System.Uri class for getting the relative path

By Shinuraj Feb 15 2011
How to find a relative path based on a base path.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 8.3k
  • 0

Suppose if we need to find a relative path based on a base path, we can use uri class in system.uri  namespace as shown below. 

           
Uri
baseUri = new Uri(basePath);
            Uri fullUri = new Uri(fullPath);
            Uri relativeUri = baseUri.MakeRelativeUri(fullUri);
For example if you are working in a folder called C:\Windows\templates\ and I want to find a relative path for C:\Windows\templates\subdir\test.txt
I want to receive back subdir\test.txt. - In other words always return a non-hardcoded path based on some other known directory.