Directory.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.IO.Directory.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. //
  7. // Author: Jim Richardson, [email protected]
  8. // Created: Monday, August 13, 2001
  9. //
  10. // TODO: Research exceptions for all methods
  11. //------------------------------------------------------------------------------
  12. using System;
  13. using System.IO.Private;
  14. using System.Security.Permissions;
  15. namespace System.IO
  16. {
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public sealed class Directory : Object
  21. {
  22. /// <summary>
  23. /// Creates all directories not existing in path
  24. /// </summary>
  25. public static DirectoryInfo CreateDirectory(string path)
  26. {
  27. DirectoryInfo dInfo = getInfo(path);
  28. if(!dInfo.Exists)
  29. {
  30. dInfo.Create();
  31. }
  32. return dInfo;
  33. }
  34. /// <summary>
  35. /// Delete an empty directory
  36. /// </summary>
  37. public static void Delete(string path)
  38. {
  39. DirectoryInfo dInfo = getInfo(path);
  40. if(dInfo.Exists)
  41. {
  42. dInfo.Delete();
  43. }
  44. }
  45. /// <summary>
  46. /// Delete a directory, and contents if bRecurse is true
  47. /// </summary>
  48. public static void Delete(string path, bool bRecurse)
  49. {
  50. DirectoryInfo dInfo = getInfo(path);
  51. if(dInfo.Exists)
  52. {
  53. dInfo.Delete(bRecurse);
  54. }
  55. }
  56. /// <summary>
  57. /// Returns true if directory exists on disk
  58. /// </summary>
  59. public static bool Exists(string path)
  60. {
  61. return getInfo(path).Exists;
  62. }
  63. /// <summary>
  64. /// Returns the date and time the directory specified by path was created
  65. /// </summary>
  66. public static DateTime GetCreationTime(string path)
  67. {
  68. return getInfo(path).CreationTime;
  69. }
  70. /// <summary>
  71. /// Returns the date and time the directory specified by path was created
  72. /// </summary>
  73. public static string GetCurrentDirectory()
  74. { // Implementation complete 08/25/2001 14:24 except for
  75. // LAMESPEC: documentation specifies invalid exceptions (i think)
  76. // also shouldn't need Write to getcurrrent should we?
  77. string str = Environment.CurrentDirectory;
  78. CheckPermission.Demand(FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, str);
  79. return str;
  80. }
  81. /// <summary>
  82. /// Returns an array of directories in the directory specified by path
  83. /// </summary>
  84. public static string[] GetDirectories(string path)
  85. {
  86. return getNames(getInfo(path).GetDirectories());
  87. }
  88. /// <summary>
  89. /// Returns an array of directories in the directory specified by path
  90. /// matching the filter specified by mask
  91. /// </summary>
  92. public static string[] GetDirectories(string path, string mask)
  93. {
  94. return getNames(DirectoryInfo.GetDirectories(mask));
  95. }
  96. /// <summary>
  97. /// Returns the root of the specified path
  98. /// </summary>
  99. public static string GetDirectoryRoot(string path)
  100. {
  101. return getInfo(path).Root.FullName;
  102. }
  103. /// <summary>
  104. /// Returns an array of files in the directory specified by path
  105. /// </summary>
  106. public static string[] GetFiles(string path)
  107. {
  108. return getNames(getInfo(path).GetFiles());
  109. }
  110. /// <summary>
  111. /// Returns an array of files in the directory specified by path
  112. /// matching the filter specified by mask
  113. /// </summary>
  114. public static string[] GetFiles(string path, string mask)
  115. {
  116. return getNames(getInfo(path).GetFiles());
  117. }
  118. /// <summary>
  119. /// Returns an array of filesystementries in the directory specified by path
  120. /// I think this is just files and directories
  121. /// </summary>
  122. public static string[] GetFileSystemEntries(string path)
  123. { // TODO: Research to verify this is files + directories
  124. return getNames(getInfo(path).GetFileSystemInfos());
  125. }
  126. /// <summary>
  127. /// Returns an array of filesystementries in the directory specified by path
  128. /// matching the filter specified by mask
  129. /// </summary>
  130. public static string[] GetFileSystemEntries(string path, string mask)
  131. { // TODO: Research to verify this is files + directories
  132. return getNames(getInfo(path).GetFileSystemInfos()); }
  133. /// <summary>
  134. /// Returns the date and time the directory specified by path was last accessed
  135. /// </summary>
  136. public static DateTime GetLastAccessTime(string path)
  137. {
  138. return getInfo(path).LastAccessTime;
  139. }
  140. /// <summary>
  141. /// Returns the date and time the directory specified by path was last modified
  142. /// </summary>
  143. public static DateTime GetLastWriteTime(string path)
  144. {
  145. return getInfo(path).LastWriteTime;
  146. }
  147. /// <summary>
  148. /// Returns an array of logical drives on this system
  149. /// </summary>
  150. public static string[] GetLogicalDrives()
  151. { // TODO: Implement
  152. return null;
  153. }
  154. /// <summary>
  155. /// Returns the parent directory of the directory specified by path
  156. /// </summary>
  157. public static DirectoryInfo GetParent(string path)
  158. { // TODO: Implement
  159. return null;
  160. }
  161. /// <summary>
  162. /// Moves a directory and its contents
  163. /// </summary>
  164. public static void Move(string src, string dst)
  165. {
  166. getInfo(src).MoveTo(dst);
  167. }
  168. /// <summary>
  169. /// Sets the creation time of the directory specified by path
  170. /// </summary>
  171. public static void SetCreationTime(string path, DateTime creationTime)
  172. {
  173. getInfo(path).CreationTime = creationTime;
  174. }
  175. /// <summary>
  176. /// Sets the current directory to the directory specified by path
  177. /// </summary>
  178. public static void SetCurrentDirectory(string path)
  179. { // Implementation complete 08/25/2001 14:24 except for
  180. // LAMESPEC: documentation specifies invalid exceptions IOException (i think)
  181. CheckArgument.Path(path, true);
  182. CheckPermission.Demand(FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, path);
  183. if(!Exists(path))
  184. {
  185. throw new DirectoryNotFoundException("Directory \"" + path + "\" not found.");
  186. }
  187. Environment.CurrentDirectory = path;
  188. }
  189. /// <summary>
  190. /// Sets the last access time of the directory specified by path
  191. /// </summary>
  192. public static void SetLastAccessTime(string path, DateTime accessTime)
  193. {
  194. getInfo(path).LastAccessTime = accessTime;
  195. }
  196. /// <summary>
  197. /// Sets the last write time of the directory specified by path
  198. /// </summary>
  199. public static void SetLastWriteTime(string path, DateTime modifiedTime)
  200. {
  201. getInfo(path).LastWriteTime = modifiedTime;
  202. }
  203. private static DirectoryInfo getInfo(string path)
  204. {
  205. return new DirectoryInfo(path);
  206. }
  207. private static string[] getNames(FileSystemInfo[] arInfo)
  208. {
  209. int index = 0;
  210. string[] ar = new string[arInfo.Length];
  211. foreach(FileInfo fi in arInfo)
  212. {
  213. ar[index++] = fi.FullName;
  214. }
  215. return ar;
  216. }
  217. }
  218. }