root / trunk / Pithos.Interfaces / FileInfoExtensions.cs @ 91b21852
History | View | Annotate | Download (6.8 kB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Diagnostics.Contracts; |
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
using System.IO; |
7 |
using System.Text.RegularExpressions; |
8 |
using System.Threading.Tasks; |
9 |
|
10 |
|
11 |
namespace Pithos.Interfaces |
12 |
{ |
13 |
public static class FileInfoExtensions |
14 |
{ |
15 |
public static string AsRelativeTo(this FileSystemInfo fileInfo,string path ) |
16 |
{ |
17 |
if (String.IsNullOrWhiteSpace(path)) |
18 |
throw new ArgumentNullException("path"); |
19 |
Contract.EndContractBlock(); |
20 |
Contract.Assume(Enum.IsDefined(typeof(StringComparison),StringComparison.InvariantCultureIgnoreCase)); |
21 |
|
22 |
if (!path.EndsWith("\\")) |
23 |
path=path.ToLower() + "\\"; |
24 |
int pathLength = path.Length; |
25 |
|
26 |
var filePath = fileInfo.GetProperCapitalization(); |
27 |
|
28 |
if (!filePath.StartsWith(path,StringComparison.InvariantCultureIgnoreCase)) |
29 |
throw new ArgumentException(String.Format("The path {0} doesn't contain the file {1}",path,filePath)); |
30 |
|
31 |
var relativePath = filePath.Substring(pathLength, filePath.Length - pathLength); |
32 |
|
33 |
return relativePath; |
34 |
} |
35 |
|
36 |
public static string AsRelativeUrlTo(this FileSystemInfo fileInfo,string path ) |
37 |
{ |
38 |
if (String.IsNullOrWhiteSpace(path)) |
39 |
throw new ArgumentNullException("path"); |
40 |
Contract.EndContractBlock(); |
41 |
|
42 |
var relativePath = fileInfo.AsRelativeTo(path); |
43 |
var replacedSlashes = relativePath.Replace("\\","/"); |
44 |
var escaped = Uri.EscapeUriString(replacedSlashes); |
45 |
return escaped; |
46 |
} |
47 |
|
48 |
public static string RelativeUriToFilePath(this Uri uri) |
49 |
{ |
50 |
var unescaped = Uri.UnescapeDataString(uri.ToString()); |
51 |
var path = unescaped.Replace("/", "\\"); |
52 |
return path; |
53 |
} |
54 |
|
55 |
|
56 |
public static string GetProperDirectoryCapitalization(string fileName) |
57 |
{ |
58 |
Contract.Ensures(!String.IsNullOrWhiteSpace(Contract.Result<string>())); |
59 |
Contract.EndContractBlock(); |
60 |
|
61 |
if (String.IsNullOrWhiteSpace(fileName)) |
62 |
return String.Empty; |
63 |
|
64 |
var dirInfo = new DirectoryInfo(fileName); |
65 |
return dirInfo.GetProperCapitalization(); |
66 |
} |
67 |
|
68 |
|
69 |
public static string GetProperCapitalization(this DirectoryInfo dirInfo) |
70 |
{ |
71 |
if (dirInfo == null) |
72 |
throw new ArgumentNullException("dirInfo"); |
73 |
Contract.EndContractBlock(); |
74 |
Contract.Assume(!String.IsNullOrWhiteSpace(dirInfo.FullName)); |
75 |
|
76 |
var parentDirInfo = dirInfo.Parent; |
77 |
if (null == parentDirInfo) |
78 |
return dirInfo.Name; |
79 |
|
80 |
try |
81 |
{ |
82 |
|
83 |
|
84 |
if (dirInfo.Exists) |
85 |
return Path.Combine(GetProperDirectoryCapitalization(parentDirInfo.FullName), |
86 |
parentDirInfo.GetDirectories(dirInfo.Name)[0].Name); |
87 |
else |
88 |
{ |
89 |
return dirInfo.FullName; |
90 |
} |
91 |
} |
92 |
catch (DirectoryNotFoundException) |
93 |
{ |
94 |
//An exception can occur if a directory is deleted right after the Exists call |
95 |
return dirInfo.FullName; |
96 |
} |
97 |
} |
98 |
|
99 |
|
100 |
public static string GetProperFilePathCapitalization(string fileName) |
101 |
{ |
102 |
Contract.Ensures(!String.IsNullOrWhiteSpace(Contract.Result<string>())); |
103 |
Contract.EndContractBlock(); |
104 |
if (String.IsNullOrWhiteSpace(fileName)) |
105 |
return String.Empty; |
106 |
|
107 |
|
108 |
var fileInfo = new FileInfo(fileName); |
109 |
return fileInfo.GetProperCapitalization(); |
110 |
} |
111 |
|
112 |
public static string GetProperCapitalization(this FileInfo fileInfo) |
113 |
{ |
114 |
if (fileInfo == null) |
115 |
throw new ArgumentNullException("fileInfo"); |
116 |
Contract.EndContractBlock(); |
117 |
|
118 |
|
119 |
var dirInfo = fileInfo.Directory; |
120 |
|
121 |
//Directory will not be null for an absolute path |
122 |
Contract.Assume(dirInfo != null); |
123 |
|
124 |
try |
125 |
{ |
126 |
//Exists returns a cached value that can be true even if a file was deleted since the |
127 |
//FileInfo object was created. |
128 |
fileInfo.Refresh(); |
129 |
if (fileInfo.Exists) |
130 |
return Path.Combine(GetProperDirectoryCapitalization(dirInfo.FullName), |
131 |
dirInfo.GetFiles(fileInfo.Name)[0].Name); |
132 |
else |
133 |
{ |
134 |
return fileInfo.FullName; |
135 |
} |
136 |
} |
137 |
catch (FileNotFoundException) |
138 |
{ |
139 |
//An exception can occur if a file is deleted right after the Exists call |
140 |
return fileInfo.FullName; |
141 |
|
142 |
} |
143 |
} |
144 |
|
145 |
public static string GetProperCapitalization(this FileSystemInfo info) |
146 |
{ |
147 |
if (info is FileInfo) |
148 |
return (info as FileInfo).GetProperCapitalization(); |
149 |
if (info is DirectoryInfo) |
150 |
return (info as DirectoryInfo).GetProperCapitalization(); |
151 |
throw new NotSupportedException("Unexpected parameter type"); |
152 |
} |
153 |
|
154 |
public static DirectoryInfo WithProperCapitalization(this DirectoryInfo dirInfo) |
155 |
{ |
156 |
if (dirInfo==null) |
157 |
throw new ArgumentNullException("dirInfo"); |
158 |
Contract.EndContractBlock(); |
159 |
|
160 |
var path = dirInfo.GetProperCapitalization(); |
161 |
return new DirectoryInfo(path); |
162 |
} |
163 |
|
164 |
public static FileInfo WithProperCapitalization(this FileInfo fileInfo) |
165 |
{ |
166 |
if (fileInfo==null) |
167 |
throw new ArgumentNullException("fileInfo"); |
168 |
Contract.EndContractBlock(); |
169 |
|
170 |
var path = fileInfo.GetProperCapitalization(); |
171 |
return new FileInfo(path); |
172 |
} |
173 |
|
174 |
public static FileSystemInfo WithProperCapitalization(this FileSystemInfo info) |
175 |
{ |
176 |
if (info==null) |
177 |
throw new ArgumentNullException("info"); |
178 |
Contract.EndContractBlock(); |
179 |
|
180 |
if (info is FileInfo) |
181 |
return (info as FileInfo).WithProperCapitalization(); |
182 |
if (info is DirectoryInfo) |
183 |
return (info as DirectoryInfo).WithProperCapitalization(); |
184 |
|
185 |
throw new NotSupportedException("Unexpected parameter type"); |
186 |
} |
187 |
|
188 |
public static FileSystemInfo FromPath(string filePath) |
189 |
{ |
190 |
if (String.IsNullOrWhiteSpace(filePath)) |
191 |
throw new ArgumentNullException("filePath"); |
192 |
Contract.EndContractBlock(); |
193 |
|
194 |
return Directory.Exists(filePath) ? |
195 |
(FileSystemInfo) new DirectoryInfo(filePath) |
196 |
: new FileInfo(filePath); |
197 |
} |
198 |
} |
199 |
} |