using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Text; using System.IO; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Pithos.Interfaces { public static class FileInfoExtensions { public static string AsRelativeTo(this FileInfo fileInfo,string path ) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); Contract.EndContractBlock(); if (!path.EndsWith("\\")) path=path.ToLower() + "\\"; int pathLength = path.Length; var filePath = fileInfo.FullName; if (!filePath.StartsWith(path,StringComparison.InvariantCultureIgnoreCase)) throw new ArgumentException(String.Format("The path {0} doesn't contain the file {1}",path,filePath)); var relativePath = filePath.Substring(pathLength, filePath.Length - pathLength); return relativePath; } public static string AsRelativeUrlTo(this FileInfo fileInfo,string path ) { if (String.IsNullOrWhiteSpace(path)) throw new ArgumentNullException("path"); Contract.EndContractBlock(); var relativePath = fileInfo.AsRelativeTo(path); var replacedSlashes = relativePath.Replace("\\","/"); var escaped = Uri.EscapeUriString(replacedSlashes); return escaped; } public static string RelativeUriToFilePath(this Uri uri) { var unescaped = Uri.UnescapeDataString(uri.ToString()); var path = unescaped.Replace("/", "\\"); return path; } } }