FileHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 FileHelper
  14. {
  15. internal const string LOCAL_FOLDER = "ms-appdata:///local/";
  16. internal const string ROAMING_FOLDER = "ms-appdata:///roaming/";
  17. internal const string TEMP_FOLDER = "ms-appdata:///temp/";
  18. internal const string STORE_FOLDER = "isostore:/";
  19. public static Stream OpenFileForReading(string uri)
  20. {
  21. return FileHelper.OpenFileForReading(FileHelper.GetFileForPathOrURI(uri));
  22. }
  23. public static Stream OpenFileForReading(System.Uri uri)
  24. {
  25. Task<StorageFile> task = WindowsRuntimeSystemExtensions.AsTask<StorageFile>(StorageFile.GetFileFromApplicationUriAsync(uri));
  26. task.Wait();
  27. if (task.Status != TaskStatus.RanToCompletion)
  28. throw new Exception("Filed to open file " + uri.ToString());
  29. else
  30. return FileHelper.OpenFileForReading(task.Result);
  31. }
  32. public static Stream OpenFileForWriting(string uri)
  33. {
  34. string fileName = Path.GetFileName(uri);
  35. Task<StorageFile> task1 = WindowsRuntimeSystemExtensions.AsTask<StorageFile>(FileHelper.GetFolderForPathOrURI(uri).CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting));
  36. task1.Wait();
  37. if (task1.Status != TaskStatus.RanToCompletion)
  38. throw new Exception("Failed to open the file");
  39. Task<IRandomAccessStream> task2 = WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(task1.Result.OpenAsync(FileAccessMode.ReadWrite));
  40. task2.Wait();
  41. if (task2.Status != TaskStatus.RanToCompletion)
  42. throw new Exception("Failed to open the file");
  43. else
  44. return WindowsRuntimeStreamExtensions.AsStreamForWrite((IOutputStream)task2.Result);
  45. }
  46. internal static StorageFolder GetFolderForURI(string uri)
  47. {
  48. uri = uri.ToLower();
  49. StorageFolder storageFolder1;
  50. if (uri.StartsWith("ms-appdata:///local/"))
  51. {
  52. storageFolder1 = ApplicationData.Current.LocalFolder;
  53. uri = uri.Replace("ms-appdata:///local/", "");
  54. }
  55. else if (uri.StartsWith("ms-appdata:///roaming/"))
  56. {
  57. storageFolder1 = ApplicationData.Current.RoamingFolder;
  58. uri = uri.Replace("ms-appdata:///roaming/", "");
  59. }
  60. else
  61. {
  62. if (!uri.StartsWith("ms-appdata:///temp/"))
  63. throw new Exception("Unsupported URI: " + uri);
  64. storageFolder1 = ApplicationData.Current.TemporaryFolder;
  65. uri = uri.Replace("ms-appdata:///temp/", "");
  66. }
  67. string[] strArray = uri.Split(new char[1]
  68. {
  69. '/'
  70. });
  71. for (int index = 0; index < strArray.Length - 1; ++index)
  72. {
  73. Task<IReadOnlyList<StorageFolder>> task = WindowsRuntimeSystemExtensions.AsTask<IReadOnlyList<StorageFolder>>(storageFolder1.CreateFolderQuery().GetFoldersAsync());
  74. task.Wait();
  75. if (task.Status != TaskStatus.RanToCompletion)
  76. throw new Exception("Failed to find folder: " + strArray[index]);
  77. IReadOnlyList<StorageFolder> result = task.Result;
  78. bool flag = false;
  79. foreach (StorageFolder storageFolder2 in (IEnumerable<StorageFolder>)result)
  80. {
  81. if (storageFolder2.Name == strArray[index])
  82. {
  83. storageFolder1 = storageFolder2;
  84. flag = true;
  85. break;
  86. }
  87. }
  88. if (!flag)
  89. throw new Exception("Folder not found: " + strArray[index]);
  90. }
  91. return storageFolder1;
  92. }
  93. internal static StorageFolder GetFolderForPathOrURI(string path)
  94. {
  95. if (System.Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute))
  96. return FileHelper.GetFolderForURI(path);
  97. IAsyncOperation<StorageFolder> folderFromPathAsync = StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(path));
  98. WindowsRuntimeSystemExtensions.AsTask<StorageFolder>(folderFromPathAsync).Wait();
  99. return folderFromPathAsync.GetResults();
  100. }
  101. internal static StorageFile GetFileForPathOrURI(string path)
  102. {
  103. IAsyncOperation<StorageFile> source = !System.Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute) ? StorageFile.GetFileFromPathAsync(path) : StorageFile.GetFileFromApplicationUriAsync(new System.Uri(path));
  104. WindowsRuntimeSystemExtensions.AsTask<StorageFile>(source).Wait();
  105. return source.GetResults();
  106. }
  107. internal static Stream OpenFileForReading(StorageFile file)
  108. {
  109. Task<IRandomAccessStream> task = WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(file.OpenAsync(FileAccessMode.Read));
  110. task.Wait();
  111. if (task.Status != TaskStatus.RanToCompletion)
  112. throw new Exception("Failed to open file!");
  113. else
  114. return WindowsRuntimeStreamExtensions.AsStreamForRead((IInputStream)task.Result);
  115. }
  116. internal static byte[] ReadEntireFile(StorageFile file)
  117. {
  118. Task<IBuffer> task = WindowsRuntimeSystemExtensions.AsTask<IBuffer>(FileIO.ReadBufferAsync((IStorageFile)file));
  119. task.Wait();
  120. if (task.Status != TaskStatus.RanToCompletion)
  121. throw new Exception("Failed to read file");
  122. IBuffer result = task.Result;
  123. DataReader dataReader = DataReader.FromBuffer(result);
  124. byte[] numArray = new byte[result.Length];
  125. dataReader.ReadBytes(numArray);
  126. return numArray;
  127. }
  128. }
  129. }
  130. #endif