Infos.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 abstract class FileSystemInfo
  14. {
  15. public FileAttributes Attributes
  16. {
  17. get
  18. {
  19. return this.GetAttributes();
  20. }
  21. }
  22. public DateTime CreationTime
  23. {
  24. get
  25. {
  26. return this.GetCreationTime().DateTime;
  27. }
  28. }
  29. public DateTime CreationTimeUtc
  30. {
  31. get
  32. {
  33. return this.GetCreationTime().ToUniversalTime().DateTime;
  34. }
  35. }
  36. public abstract bool Exists { get; }
  37. public string Extention
  38. {
  39. get
  40. {
  41. return Path.GetExtension(this.FullName);
  42. }
  43. }
  44. public abstract string FullName { get; }
  45. public abstract string Name { get; }
  46. internal abstract FileAttributes GetAttributes();
  47. internal abstract DateTimeOffset GetCreationTime();
  48. public abstract void Delete();
  49. public void Refresh()
  50. {
  51. this.RefreshInternal();
  52. }
  53. internal abstract void RefreshInternal();
  54. }
  55. public sealed class DirectoryInfo : FileSystemInfo
  56. {
  57. private string path;
  58. private StorageFolder folder;
  59. public override bool Exists
  60. {
  61. get
  62. {
  63. try
  64. {
  65. this.RefreshInternal();
  66. return true;
  67. }
  68. catch
  69. {
  70. return false;
  71. }
  72. }
  73. }
  74. public override string FullName
  75. {
  76. get
  77. {
  78. return this.folder.Path;
  79. }
  80. }
  81. public override string Name
  82. {
  83. get
  84. {
  85. return this.folder.Name;
  86. }
  87. }
  88. public DirectoryInfo(string path)
  89. {
  90. if (path == null)
  91. throw new ArgumentNullException();
  92. if (string.IsNullOrWhiteSpace(path))
  93. throw new ArgumentException();
  94. try
  95. {
  96. this.path = path;
  97. this.folder = FileHelper.GetFolderForPathOrURI(path);
  98. }
  99. catch (IOException ex)
  100. {
  101. System.Diagnostics.Debug.WriteLine("DirectoryInfo: " + ex.Message + "\n" + ex.StackTrace);
  102. throw;
  103. }
  104. catch (Exception ex)
  105. {
  106. System.Diagnostics.Debug.WriteLine("DirectoryInfo: " + ex.Message + "\n" + ex.StackTrace);
  107. throw new IOException(ex.Message, ex);
  108. }
  109. }
  110. internal DirectoryInfo(string path, StorageFolder folder)
  111. {
  112. this.path = path;
  113. this.folder = folder;
  114. }
  115. internal override FileAttributes GetAttributes()
  116. {
  117. try
  118. {
  119. return File.WinAttributesToSysAttributes(this.folder.Attributes);
  120. }
  121. catch (IOException ex)
  122. {
  123. System.Diagnostics.Debug.WriteLine("DirectoryInfo.GetAttributes: " + ex.Message + "\n" + ex.StackTrace);
  124. throw;
  125. }
  126. catch (Exception ex)
  127. {
  128. System.Diagnostics.Debug.WriteLine("DirectoryInfo.GetAttributes: " + ex.Message + "\n" + ex.StackTrace);
  129. throw new IOException(ex.Message, ex);
  130. }
  131. }
  132. internal override DateTimeOffset GetCreationTime()
  133. {
  134. try
  135. {
  136. return this.folder.DateCreated;
  137. }
  138. catch (IOException ex)
  139. {
  140. System.Diagnostics.Debug.WriteLine("DirectoryInfo.GetCreationTime: " + ex.Message + "\n" + ex.StackTrace);
  141. throw;
  142. }
  143. catch (Exception ex)
  144. {
  145. System.Diagnostics.Debug.WriteLine("DirectoryInfo.GetCreationTime: " + ex.Message + "\n" + ex.StackTrace);
  146. throw new IOException(ex.Message, ex);
  147. }
  148. }
  149. public override void Delete()
  150. {
  151. try
  152. {
  153. WindowsRuntimeSystemExtensions.AsTask(this.folder.DeleteAsync()).Wait();
  154. }
  155. catch (IOException ex)
  156. {
  157. System.Diagnostics.Debug.WriteLine("DirectoryInfo.Delete: " + ex.Message + "\n" + ex.StackTrace);
  158. throw;
  159. }
  160. catch (Exception ex)
  161. {
  162. System.Diagnostics.Debug.WriteLine("DirectoryInfo.Delete: " + ex.Message + "\n" + ex.StackTrace);
  163. throw new IOException(ex.Message, ex);
  164. }
  165. }
  166. internal override void RefreshInternal()
  167. {
  168. try
  169. {
  170. this.folder = FileHelper.GetFolderForPathOrURI(this.path);
  171. }
  172. catch (IOException ex)
  173. {
  174. System.Diagnostics.Debug.WriteLine("DirectoryInfo.RefreshInternal: " + ex.Message + "\n" + ex.StackTrace);
  175. throw;
  176. }
  177. catch (Exception ex)
  178. {
  179. System.Diagnostics.Debug.WriteLine("DirectoryInfo.RefreshInternal: " + ex.Message + "\n" + ex.StackTrace);
  180. throw new IOException(ex.Message, ex);
  181. }
  182. }
  183. public override string ToString()
  184. {
  185. return this.path;
  186. }
  187. public override int GetHashCode()
  188. {
  189. return this.path.GetHashCode();
  190. }
  191. }
  192. }
  193. #endif