User Tools

Site Tools


snippets:paths

Absolute and relative path manipulation

Assembly (reference)

System

Using directive

using System.IO;

Code to convert to an absolute path

Converts path to an absolute path relatively to the basePath. The code also works properly if the path is already absolute. If the basePath is not provided and the path is relative, the original relative path is returned. This behaviour can be easily modified.

        public static String ToAbsolutePath(String basePath, String path) {
            if (String.IsNullOrEmpty(path))
                return null;
            if (String.IsNullOrEmpty(basePath))
                return path;
 
            Uri path2 = new Uri(path, UriKind.RelativeOrAbsolute);
            if (path2.IsAbsoluteUri)
                return path;
            Uri basePath = new Uri(basePath + "/", UriKind.Absolute);
            Uri absPath = new Uri(basePath, path);
            return absPath.LocalPath;
        }

Code to convert to a relative path

Similarly this code converts path to a path relative to basePath, if possible. Note the Uri.UnescapeDataString call to avoid issues with spaces or special characters in paths.

        public static String ToRelativePath(String basePath, String path) {
            if (String.IsNullOrEmpty(path))
                return null;
            if (String.IsNullOrEmpty(basePath))
                return path;
 
            Uri uri1 = new Uri(path, UriKind.RelativeOrAbsolute);
            if (uri1.IsAbsoluteUri)
            {
                Uri uri2 = new Uri(basePath + "/", UriKind.Absolute);
                Uri relativeUri = uri2.MakeRelativeUri(uri1);
                return Uri.UnescapeDataString(relativeUri.ToString()).Replace('/', Path.DirectorySeparatorChar);
            }
            // else it is already a relative path
            return path;
        }
snippets/paths.txt · Last modified: 2015/07/26 14:43 by jaap.dehaan