2 using System.Collections.Generic;
3 using System.Diagnostics.Contracts;
7 using System.Text.RegularExpressions;
8 using System.Threading.Tasks;
11 namespace Pithos.Interfaces
13 public static class FileInfoExtensions
15 public static string AsRelativeTo(this FileSystemInfo fileInfo,string path )
17 if (String.IsNullOrWhiteSpace(path))
18 throw new ArgumentNullException("path");
19 Contract.EndContractBlock();
20 Contract.Assume(Enum.IsDefined(typeof(StringComparison),StringComparison.InvariantCultureIgnoreCase));
22 if (!path.EndsWith("\\"))
23 path=path.ToLower() + "\\";
24 int pathLength = path.Length;
26 var filePath = fileInfo.GetProperCapitalization();
28 if (!filePath.StartsWith(path,StringComparison.InvariantCultureIgnoreCase))
29 throw new ArgumentException(String.Format("The path {0} doesn't contain the file {1}",path,filePath));
31 var relativePath = filePath.Substring(pathLength, filePath.Length - pathLength);
36 public static string AsRelativeUrlTo(this FileSystemInfo fileInfo,string path )
38 if (String.IsNullOrWhiteSpace(path))
39 throw new ArgumentNullException("path");
40 Contract.EndContractBlock();
42 var relativePath = fileInfo.AsRelativeTo(path);
43 var replacedSlashes = relativePath.Replace("\\","/");
44 var escaped = Uri.EscapeUriString(replacedSlashes);
48 public static string RelativeUriToFilePath(this Uri uri)
50 var unescaped = Uri.UnescapeDataString(uri.ToString());
51 var path = unescaped.Replace("/", "\\");
56 public static string GetProperDirectoryCapitalization(string fileName)
58 if (String.IsNullOrWhiteSpace(fileName))
59 throw new ArgumentNullException("fileName");
60 if (!Path.IsPathRooted(fileName))
61 throw new ArgumentException("fileName must be an absolute path", "fileName");
62 Contract.EndContractBlock();
64 var dirInfo = new DirectoryInfo(fileName);
65 return dirInfo.GetProperCapitalization();
68 public static string GetProperCapitalization(this DirectoryInfo dirInfo)
71 throw new ArgumentNullException("dirInfo");
72 Contract.EndContractBlock();
74 var parentDirInfo = dirInfo.Parent;
75 if (null == parentDirInfo)
83 return Path.Combine(GetProperDirectoryCapitalization(parentDirInfo.FullName),
84 parentDirInfo.GetDirectories(dirInfo.Name)[0].Name);
87 return dirInfo.FullName;
90 catch (DirectoryNotFoundException)
92 //An exception can occur if a directory is deleted right after the Exists call
93 return dirInfo.FullName;
98 public static string GetProperFilePathCapitalization(string fileName)
100 if (String.IsNullOrWhiteSpace(fileName))
101 throw new ArgumentNullException("fileName");
102 if (!Path.IsPathRooted(fileName))
103 throw new ArgumentException("fileName must be an absolute path", "fileName");
104 Contract.EndContractBlock();
107 var fileInfo = new FileInfo(fileName);
108 return fileInfo.GetProperCapitalization();
111 public static string GetProperCapitalization(this FileInfo fileInfo)
113 if (fileInfo == null)
114 throw new ArgumentNullException("fileInfo");
115 Contract.EndContractBlock();
118 var dirInfo = fileInfo.Directory;
120 //Directory will not be null for an absolute path
121 Contract.Assume(dirInfo != null);
127 return Path.Combine(GetProperDirectoryCapitalization(dirInfo.FullName),
128 dirInfo.GetFiles(fileInfo.Name)[0].Name);
131 return fileInfo.FullName;
134 catch (FileNotFoundException)
136 //An exception can occur if a file is deleted right after the Exists call
137 return fileInfo.FullName;
142 public static string GetProperCapitalization(this FileSystemInfo info)
144 if (info is FileInfo)
145 return (info as FileInfo).GetProperCapitalization();
146 if (info is DirectoryInfo)
147 return (info as DirectoryInfo).GetProperCapitalization();
148 throw new NotSupportedException("Unexpected parameter type");
151 public static DirectoryInfo WithProperCapitalization(this DirectoryInfo dirInfo)
154 throw new ArgumentNullException("dirInfo");
155 Contract.EndContractBlock();
157 var path = dirInfo.GetProperCapitalization();
158 return new DirectoryInfo(path);
161 public static FileInfo WithProperCapitalization(this FileInfo fileInfo)
164 throw new ArgumentNullException("fileInfo");
165 Contract.EndContractBlock();
167 var path = fileInfo.GetProperCapitalization();
168 return new FileInfo(path);
171 public static FileSystemInfo WithProperCapitalization(this FileSystemInfo info)
174 throw new ArgumentNullException("info");
175 Contract.EndContractBlock();
177 if (info is FileInfo)
178 return (info as FileInfo).WithProperCapitalization();
179 if (info is DirectoryInfo)
180 return (info as DirectoryInfo).WithProperCapitalization();
182 throw new NotSupportedException("Unexpected parameter type");