Exemplo n.º 1
0
        // Best way to convert absolute path to relative
        // Author: https://stackoverflow.com/a/485516
        // For example, input:
        // ["C:\Windows\System32\", "C:\Windows\System32\drivers\etc\hosts"]
        // output: ".\drivers\etc\hosts"
        public static string GetRelativePath(string fromPath, string toPath)
        {
            int fromAttr = RelativePath.GetPathAttribute(fromPath);
            int toAttr   = RelativePath.GetPathAttribute(toPath);

            StringBuilder path = new StringBuilder(260); // MAX_PATH

            if (RelativePath.PathRelativePathTo(
                    path,
                    fromPath,
                    fromAttr,
                    toPath,
                    toAttr) == 0)
            {
                throw new ArgumentException("Paths must have a common prefix");
            }
            var output = path.ToString();

            output = output.Substring(2, output.Length - 2);
            return(output);
        }