File.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #if NETFX_CORE
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Windows.Storage;
  9. using Windows.Storage.Streams;
  10. using Windows.Foundation;
  11. namespace BestHTTP.PlatformSupport.IO
  12. {
  13. public static class File
  14. {
  15. public static void AppendAllLines(string path, IEnumerable<string> contents)
  16. {
  17. File.AppendAllLines(path, contents, (Encoding)null);
  18. }
  19. public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  20. {
  21. try
  22. {
  23. IEnumerator<string> enumerator = contents.GetEnumerator();
  24. if (!enumerator.MoveNext())
  25. return;
  26. using (StreamWriter streamWriter = File.AppendText(path, encoding))
  27. {
  28. string current = enumerator.Current;
  29. while (enumerator.MoveNext())
  30. {
  31. streamWriter.WriteLine(current);
  32. current = enumerator.Current;
  33. }
  34. streamWriter.Write(current);
  35. }
  36. }
  37. catch (Exception ex)
  38. {
  39. throw File.GetRethrowException(ex);
  40. }
  41. }
  42. public static void AppendAllText(string path, string contents)
  43. {
  44. File.AppendAllText(path, contents, (Encoding)null);
  45. }
  46. public static void AppendAllText(string path, string contents, Encoding encoding)
  47. {
  48. try
  49. {
  50. using (StreamWriter streamWriter = File.AppendText(path, encoding))
  51. streamWriter.Write(contents);
  52. }
  53. catch (Exception ex)
  54. {
  55. throw File.GetRethrowException(ex);
  56. }
  57. }
  58. public static StreamWriter AppendText(string path, Encoding encoding)
  59. {
  60. try
  61. {
  62. IAsyncOperation<StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(path).CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.OpenIfExists);
  63. WindowsRuntimeSystemExtensions.AsTask<StorageFile>(fileAsync).Wait();
  64. IAsyncOperation<IRandomAccessStream> source = fileAsync.GetResults().OpenAsync(FileAccessMode.ReadWrite);
  65. WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(source).Wait();
  66. FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults();
  67. randomAccessStream.Seek(randomAccessStream.Size);
  68. return encoding == null ? new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream)) : new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream), encoding);
  69. }
  70. catch (Exception ex)
  71. {
  72. throw File.GetRethrowException(ex);
  73. }
  74. }
  75. private static Exception GetRethrowException(Exception ex)
  76. {
  77. System.Diagnostics.Debug.WriteLine("File.GetRethrowException: " + ex.Message + "\n" + ex.StackTrace);
  78. if (ex.GetType() == typeof(IOException))
  79. return ex;
  80. else
  81. return (Exception)new IOException(ex.Message, ex);
  82. }
  83. public static void Copy(string sourceFileName, string destFileName)
  84. {
  85. File.Copy(sourceFileName, destFileName, false);
  86. }
  87. public static void Copy(string sourceFileName, string destFileName, bool overwrite)
  88. {
  89. try
  90. {
  91. StorageFile fileForPathOrUri = FileHelper.GetFileForPathOrURI(sourceFileName);
  92. if (overwrite)
  93. {
  94. StorageFile storageFile = (StorageFile)null;
  95. try
  96. {
  97. storageFile = FileHelper.GetFileForPathOrURI(destFileName);
  98. }
  99. catch
  100. {
  101. }
  102. if (storageFile != null)
  103. {
  104. WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri.CopyAndReplaceAsync((IStorageFile)storageFile)).Wait();
  105. return;
  106. }
  107. }
  108. WindowsRuntimeSystemExtensions.AsTask<StorageFile>(fileForPathOrUri.CopyAsync((IStorageFolder)FileHelper.GetFolderForPathOrURI(destFileName), Path.GetFileName(destFileName))).Wait();
  109. }
  110. catch (Exception ex)
  111. {
  112. throw File.GetRethrowException(ex);
  113. }
  114. }
  115. public static FileStream Create(string path)
  116. {
  117. return new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
  118. }
  119. public static FileStream Create(string path, int bufferSize)
  120. {
  121. return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize);
  122. }
  123. public static StreamWriter CreateText(string path)
  124. {
  125. return new StreamWriter((Stream)File.Create(path));
  126. }
  127. public static void Delete(string path)
  128. {
  129. if (path == null)
  130. throw new ArgumentNullException();
  131. if (path.Trim() == "")
  132. throw new ArgumentException();
  133. try
  134. {
  135. WindowsRuntimeSystemExtensions.AsTask(FileHelper.GetFileForPathOrURI(path).DeleteAsync()).Wait();
  136. }
  137. catch (Exception ex)
  138. {
  139. throw File.GetRethrowException(ex);
  140. }
  141. }
  142. public static bool Exists(string path)
  143. {
  144. try
  145. {
  146. return FileHelper.GetFileForPathOrURI(path) != null;
  147. }
  148. catch
  149. {
  150. return false;
  151. }
  152. }
  153. public static FileAttributes GetAttributes(string path)
  154. {
  155. if (string.IsNullOrWhiteSpace(path))
  156. throw new ArgumentException();
  157. try
  158. {
  159. return File.WinAttributesToSysAttributes(FileHelper.GetFileForPathOrURI(path).Attributes);
  160. }
  161. catch (Exception ex)
  162. {
  163. throw new FileNotFoundException(ex.Message, ex);
  164. }
  165. }
  166. internal static FileAttributes WinAttributesToSysAttributes(Windows.Storage.FileAttributes atts)
  167. {
  168. FileAttributes fileAttributes = (FileAttributes)0;
  169. if ((Windows.Storage.FileAttributes.ReadOnly & atts) != Windows.Storage.FileAttributes.Normal)
  170. fileAttributes |= FileAttributes.ReadOnly;
  171. if ((Windows.Storage.FileAttributes.Directory & atts) != Windows.Storage.FileAttributes.Normal)
  172. fileAttributes |= FileAttributes.Directory;
  173. if ((Windows.Storage.FileAttributes.Archive & atts) != Windows.Storage.FileAttributes.Normal)
  174. fileAttributes |= FileAttributes.Archive;
  175. if ((Windows.Storage.FileAttributes.Temporary & atts) != Windows.Storage.FileAttributes.Normal)
  176. fileAttributes |= FileAttributes.Temporary;
  177. if (fileAttributes == (FileAttributes)0)
  178. fileAttributes = FileAttributes.Normal;
  179. return fileAttributes;
  180. }
  181. public static System.DateTime GetCreationTime(string path)
  182. {
  183. if (string.IsNullOrWhiteSpace(path))
  184. throw new ArgumentException();
  185. try
  186. {
  187. return FileHelper.GetFileForPathOrURI(path).DateCreated.DateTime;
  188. }
  189. catch (Exception ex)
  190. {
  191. throw new FileNotFoundException(ex.Message, ex);
  192. }
  193. }
  194. public static System.DateTime GetCreationTimeUtc(string path)
  195. {
  196. if (string.IsNullOrWhiteSpace(path))
  197. throw new ArgumentException();
  198. try
  199. {
  200. return FileHelper.GetFileForPathOrURI(path).DateCreated.ToUniversalTime().DateTime;
  201. }
  202. catch (Exception ex)
  203. {
  204. throw new FileNotFoundException(ex.Message, ex);
  205. }
  206. }
  207. public static void Move(string sourceFileName, string destFileName)
  208. {
  209. try
  210. {
  211. WindowsRuntimeSystemExtensions.AsTask(FileHelper.GetFileForPathOrURI(sourceFileName).MoveAsync((IStorageFolder)FileHelper.GetFolderForPathOrURI(destFileName), Path.GetFileName(destFileName))).Wait();
  212. }
  213. catch (Exception ex)
  214. {
  215. throw File.GetRethrowException(ex);
  216. }
  217. }
  218. public static FileStream Open(string path, FileMode mode)
  219. {
  220. return new FileStream(path, mode);
  221. }
  222. public static FileStream Open(string path, FileMode mode, FileAccess access)
  223. {
  224. return new FileStream(path, mode, access);
  225. }
  226. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
  227. {
  228. return new FileStream(path, mode, access, share);
  229. }
  230. public static FileStream OpenRead(string path)
  231. {
  232. return File.Open(path, FileMode.Open, FileAccess.Read);
  233. }
  234. public static StreamReader OpenText(string path)
  235. {
  236. return new StreamReader((Stream)File.OpenRead(path));
  237. }
  238. public static FileStream OpenWrite(string path)
  239. {
  240. return File.Open(path, FileMode.Create, FileAccess.Write);
  241. }
  242. public static byte[] ReadAllBytes(string path)
  243. {
  244. if (string.IsNullOrWhiteSpace(path))
  245. throw new ArgumentException();
  246. try
  247. {
  248. return FileHelper.ReadEntireFile(FileHelper.GetFileForPathOrURI(path));
  249. }
  250. catch (Exception ex)
  251. {
  252. throw File.GetRethrowException(ex);
  253. }
  254. }
  255. public static string[] ReadAllLines(string path)
  256. {
  257. return Enumerable.ToArray<string>(File.ReadLines(path));
  258. }
  259. public static string[] ReadAllLines(string path, Encoding encoding)
  260. {
  261. return File.ReadAllLines(path);
  262. }
  263. public static string ReadAllText(string path)
  264. {
  265. if (string.IsNullOrWhiteSpace(path))
  266. throw new ArgumentException();
  267. try
  268. {
  269. IAsyncOperation<IRandomAccessStream> source = FileHelper.GetFileForPathOrURI(path).OpenAsync(FileAccessMode.Read);
  270. WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(source).Wait();
  271. using (FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults())
  272. return new StreamReader(WindowsRuntimeStreamExtensions.AsStreamForRead((IInputStream)randomAccessStream)).ReadToEnd();
  273. }
  274. catch (Exception ex)
  275. {
  276. throw File.GetRethrowException(ex);
  277. }
  278. }
  279. public static string ReadAllText(string path, Encoding encoding)
  280. {
  281. return File.ReadAllText(path);
  282. }
  283. public static IEnumerable<string> ReadLines(string path)
  284. {
  285. if (string.IsNullOrWhiteSpace(path))
  286. throw new ArgumentException();
  287. try
  288. {
  289. IAsyncOperation<IRandomAccessStream> source = FileHelper.GetFileForPathOrURI(path).OpenAsync(FileAccessMode.Read);
  290. WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(source).Wait();
  291. using (FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults())
  292. {
  293. StreamReader streamReader = new StreamReader(WindowsRuntimeStreamExtensions.AsStreamForRead((IInputStream)randomAccessStream));
  294. List<string> list = new List<string>();
  295. while (true)
  296. {
  297. string str = streamReader.ReadLine();
  298. if (str != null)
  299. list.Add(str);
  300. else
  301. break;
  302. }
  303. return (IEnumerable<string>)list;
  304. }
  305. }
  306. catch (Exception ex)
  307. {
  308. throw File.GetRethrowException(ex);
  309. }
  310. }
  311. public static IEnumerable<string> ReadLines(string path, Encoding encoding)
  312. {
  313. return File.ReadLines(path);
  314. }
  315. public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName)
  316. {
  317. if (destinationFileName == null)
  318. throw new ArgumentNullException();
  319. if (!string.IsNullOrWhiteSpace(sourceFileName) && !string.IsNullOrWhiteSpace(destinationFileName))
  320. {
  321. if (!string.IsNullOrWhiteSpace(destinationBackupFileName))
  322. {
  323. try
  324. {
  325. StorageFile fileForPathOrUri1 = FileHelper.GetFileForPathOrURI(sourceFileName);
  326. string fileName1 = Path.GetFileName(destinationFileName);
  327. StorageFile fileForPathOrUri2;
  328. if (fileName1.ToLower() == destinationFileName.ToLower())
  329. {
  330. if (Path.GetFileName(sourceFileName).ToLower() == fileName1.ToLower())
  331. {
  332. System.Diagnostics.Debug.WriteLine("File.Replace: Source and destination is the same file");
  333. throw new IOException("Source and destination is the same file");
  334. }
  335. fileForPathOrUri2 = FileHelper.GetFileForPathOrURI(Path.Combine(Path.GetDirectoryName(fileForPathOrUri1.Path), fileName1));
  336. }
  337. else
  338. {
  339. fileForPathOrUri2 = FileHelper.GetFileForPathOrURI(destinationFileName);
  340. if (fileForPathOrUri1.Equals((object)fileForPathOrUri2))
  341. {
  342. System.Diagnostics.Debug.WriteLine("File.Replace: Source and destination is the same file");
  343. throw new IOException("Source and destination is the same file");
  344. }
  345. }
  346. string fileName2 = Path.GetFileName(destinationBackupFileName);
  347. if (fileName2.ToLower() == destinationBackupFileName.ToLower())
  348. {
  349. WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri2.RenameAsync(destinationBackupFileName)).Wait();
  350. }
  351. else
  352. {
  353. StorageFolder folderForPathOrUri = FileHelper.GetFolderForPathOrURI(destinationBackupFileName);
  354. WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri2.MoveAsync((IStorageFolder)folderForPathOrUri, fileName2)).Wait();
  355. }
  356. if (fileName1.ToLower() == destinationFileName.ToLower())
  357. {
  358. WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri1.RenameAsync(destinationFileName)).Wait();
  359. return;
  360. }
  361. else
  362. {
  363. StorageFolder folderForPathOrUri = FileHelper.GetFolderForPathOrURI(destinationFileName);
  364. WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri1.MoveAsync((IStorageFolder)folderForPathOrUri, fileName1)).Wait();
  365. return;
  366. }
  367. }
  368. catch (Exception ex)
  369. {
  370. throw File.GetRethrowException(ex);
  371. }
  372. }
  373. }
  374. throw new ArgumentException();
  375. }
  376. public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
  377. {
  378. File.Replace(sourceFileName, destinationFileName, destinationBackupFileName);
  379. }
  380. private static StorageFile CreateOrReplaceFile(string path)
  381. {
  382. IAsyncOperation<StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(path).CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.ReplaceExisting);
  383. WindowsRuntimeSystemExtensions.AsTask<StorageFile>(fileAsync).Wait();
  384. return fileAsync.GetResults();
  385. }
  386. public static void WriteAllBytes(string path, byte[] bytes)
  387. {
  388. if (path == null || bytes == null || bytes.Length == 0)
  389. throw new ArgumentNullException();
  390. if (string.IsNullOrWhiteSpace(path))
  391. throw new ArgumentException();
  392. try
  393. {
  394. WindowsRuntimeSystemExtensions.AsTask(FileIO.WriteBytesAsync((IStorageFile)File.CreateOrReplaceFile(path), bytes)).Wait();
  395. }
  396. catch (Exception ex)
  397. {
  398. throw File.GetRethrowException(ex);
  399. }
  400. }
  401. public static void WriteAllLines(string path, IEnumerable<string> contents)
  402. {
  403. if (path == null || contents == null)
  404. throw new ArgumentNullException();
  405. if (string.IsNullOrWhiteSpace(path))
  406. throw new ArgumentException();
  407. try
  408. {
  409. WindowsRuntimeSystemExtensions.AsTask(FileIO.WriteLinesAsync((IStorageFile)File.CreateOrReplaceFile(path), contents)).Wait();
  410. }
  411. catch (Exception ex)
  412. {
  413. throw File.GetRethrowException(ex);
  414. }
  415. }
  416. public static void WriteAllLines(string path, string[] contents)
  417. {
  418. IEnumerable<string> contents1 = (IEnumerable<string>)contents;
  419. File.WriteAllLines(path, contents1);
  420. }
  421. public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  422. {
  423. File.WriteAllLines(path, contents);
  424. }
  425. public static void WriteAllLines(string path, string[] contents, Encoding encoding)
  426. {
  427. File.WriteAllLines(path, contents);
  428. }
  429. public static void WriteAllText(string path, string contents)
  430. {
  431. if (path == null || string.IsNullOrEmpty(contents))
  432. throw new ArgumentNullException();
  433. if (string.IsNullOrWhiteSpace(path))
  434. throw new ArgumentException();
  435. try
  436. {
  437. WindowsRuntimeSystemExtensions.AsTask(FileIO.WriteTextAsync((IStorageFile)File.CreateOrReplaceFile(path), contents)).Wait();
  438. }
  439. catch (Exception ex)
  440. {
  441. throw File.GetRethrowException(ex);
  442. }
  443. }
  444. public static void WriteAllText(string path, string contents, Encoding encoding)
  445. {
  446. File.WriteAllText(path, contents);
  447. }
  448. }
  449. }
  450. #endif