FileSupport.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. namespace Wayne.Lib.IO
  5. {
  6. /// <summary>
  7. /// Support class for files.
  8. /// </summary>
  9. public static class FileSupport
  10. {
  11. #region Static fields
  12. /// <summary>
  13. /// Default encoding for reading and writing text files.
  14. /// </summary>
  15. public static readonly System.Text.Encoding DefaultEncoding = System.Text.Encoding.UTF8;
  16. /// <summary>
  17. /// Static instance of FileSupport.
  18. /// </summary>
  19. public static IFileSupport fileSupport = new FileSupportNonStatic();
  20. /// <summary>
  21. /// Platform-dependent extension of file support.
  22. /// </summary>
  23. public static IFileSupportExtension FileSupportExtension
  24. {
  25. get { return fileSupport.FileSupportExtension; }
  26. set { fileSupport.FileSupportExtension = value; }
  27. }
  28. /// <summary>
  29. /// Secure delete support extension. Dependent on the platform.
  30. /// </summary>
  31. public static ISecureDeleteSupport SecureDeleteSupport
  32. {
  33. get { return fileSupport.SecureDeleteSupport; }
  34. set { fileSupport.SecureDeleteSupport = value; }
  35. }
  36. #endregion
  37. #region Open
  38. /// <summary>
  39. /// Opens a file.
  40. /// </summary>
  41. /// <param name="fileName"></param>
  42. /// <param name="fileMode"></param>
  43. /// <param name="fileAccess"></param>
  44. /// <param name="fileShare"></param>
  45. /// <param name="retries"></param>
  46. /// <param name="delayBetweenRetries"></param>
  47. /// <returns></returns>
  48. public static Stream Open(string fileName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare, int retries, int delayBetweenRetries)
  49. {
  50. return fileSupport.Open(fileName, fileMode, fileAccess, fileShare, retries, delayBetweenRetries);
  51. }
  52. /// <summary>
  53. /// Opens a file, using standard values for retries (100) and delayBetweenRetries(100)
  54. /// </summary>
  55. /// <param name="fileName"></param>
  56. /// <param name="fileMode"></param>
  57. /// <param name="fileAccess"></param>
  58. /// <param name="fileShare"></param>
  59. /// <returns></returns>
  60. public static Stream Open(string fileName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
  61. {
  62. return fileSupport.Open(fileName, fileMode, fileAccess, fileShare);
  63. }
  64. #endregion
  65. #region Move
  66. /// <summary>
  67. /// Moves a file.
  68. /// </summary>
  69. /// <param name="sourceFileName"></param>
  70. /// <param name="destinationFileName"></param>
  71. /// <param name="retries"></param>
  72. /// <param name="delayBetweenRetries"></param>
  73. public static void Move(string sourceFileName, string destinationFileName, int retries, int delayBetweenRetries)
  74. {
  75. fileSupport.Move(sourceFileName, destinationFileName, retries, delayBetweenRetries);
  76. }
  77. /// <summary>
  78. /// Moves a file, using standard values for retries (100) and delayBetweenRetries(100)
  79. /// </summary>
  80. /// <param name="sourceFileName"></param>
  81. /// <param name="destinationFileName"></param>
  82. public static void Move(string sourceFileName, string destinationFileName)
  83. {
  84. fileSupport.Move(sourceFileName, destinationFileName);
  85. }
  86. #endregion
  87. #region Delete
  88. /// <summary>
  89. /// Deletes a file
  90. /// </summary>
  91. /// <param name="fileName"></param>
  92. /// <param name="retries"></param>
  93. /// <param name="delayBetweenRetries"></param>
  94. public static void Delete(string fileName, int retries, int delayBetweenRetries)
  95. {
  96. fileSupport.Delete(fileName, retries, delayBetweenRetries);
  97. }
  98. /// <summary>
  99. /// Deletes a file, using standard values for retries (100) and delayBetweenRetries(100)
  100. /// </summary>
  101. /// <param name="fileName"></param>
  102. [System.Diagnostics.DebuggerStepThrough]
  103. public static void Delete(string fileName)
  104. {
  105. fileSupport.Delete(fileName);
  106. }
  107. #endregion
  108. #region SecureDelete
  109. /// <summary>
  110. /// Deletes a file securely.
  111. /// </summary>
  112. /// <param name="fileName"></param>
  113. /// <param name="sDeleteOK"></param>
  114. /// <param name="retries"></param>
  115. /// <param name="delayBetweenRetries"></param>
  116. public static void SecureDelete(string fileName, out bool sDeleteOK, int retries, int delayBetweenRetries)
  117. {
  118. fileSupport.SecureDelete(fileName, out sDeleteOK, retries, delayBetweenRetries);
  119. }
  120. /// <summary>
  121. /// Deletes a file securely.
  122. /// </summary>
  123. /// <param name="fileName"></param>
  124. /// <param name="sDeleteOK"></param>
  125. public static void SecureDelete(string fileName, out bool sDeleteOK)
  126. {
  127. fileSupport.SecureDelete(fileName, out sDeleteOK);
  128. }
  129. #endregion
  130. #region Copy
  131. /// <summary>
  132. /// Copy a file.
  133. /// </summary>
  134. /// <param name="sourceFileName"></param>
  135. /// <param name="destinationFileName"></param>
  136. /// <param name="overwrite"></param>
  137. public static void Copy(string sourceFileName, string destinationFileName, bool overwrite)
  138. {
  139. fileSupport.Copy(sourceFileName, destinationFileName, overwrite);
  140. }
  141. /// <summary>
  142. /// Copies a file.
  143. /// </summary>
  144. /// <param name="sourceFileName"></param>
  145. /// <param name="destinationFileName"></param>
  146. /// <param name="overwrite"></param>
  147. /// <param name="retries"></param>
  148. /// <param name="delayBetweenRetries"></param>
  149. public static void Copy(string sourceFileName, string destinationFileName, bool overwrite, int retries, int delayBetweenRetries)
  150. {
  151. fileSupport.Copy(sourceFileName, destinationFileName, overwrite, retries, delayBetweenRetries);
  152. }
  153. #endregion
  154. #region LoadFromFile/SaveToFile
  155. /// <summary>
  156. /// Read the lines of a text file into a string.
  157. /// </summary>
  158. /// <param name="fileName">The path and file name.</param>
  159. public static string LoadToString(string fileName)
  160. {
  161. return fileSupport.LoadToString(fileName, DefaultEncoding);
  162. }
  163. /// <summary>
  164. /// Read the lines of a text file into a string.
  165. /// </summary>
  166. /// <param name="fileName">The path and file name.</param>
  167. /// <param name="encoding">The encoding.</param>
  168. public static string LoadToString(string fileName, System.Text.Encoding encoding)
  169. {
  170. return fileSupport.LoadToString(fileName, encoding);
  171. }
  172. /// <summary>
  173. /// Read the lines of a text file into an array of strings.
  174. /// </summary>
  175. /// <param name="fileName">The path and file name.</param>
  176. public static string[] LoadToStringArray(string fileName)
  177. {
  178. return fileSupport.LoadToStringArray(fileName);
  179. }
  180. /// <summary>
  181. /// Read the lines of a text file into an array of strings.
  182. /// </summary>
  183. /// <param name="fileName">The path and file name.</param>
  184. /// <param name="encoding">The encoding.</param>
  185. public static string[] LoadToStringArray(string fileName, System.Text.Encoding encoding)
  186. {
  187. return fileSupport.LoadToStringArray(fileName, encoding);
  188. }
  189. /// <summary>
  190. /// Saves a text string to a file.
  191. /// </summary>
  192. /// <param name="fileName">The path and file name.</param>
  193. /// <param name="text">The text to save.</param>
  194. public static void SaveToFile(string fileName, string text)
  195. {
  196. fileSupport.SaveToFile(fileName, text);
  197. }
  198. /// <summary>
  199. /// Saves a text string to a file.
  200. /// </summary>
  201. /// <param name="fileName">The path and file name.</param>
  202. /// <param name="lines">The lines to save.</param>
  203. public static void SaveToFile(string fileName, string[] lines)
  204. {
  205. fileSupport.SaveToFile(fileName, lines);
  206. }
  207. /// <summary>
  208. /// Saves a text string to a file.
  209. /// </summary>
  210. /// <param name="fileName">The path and file name.</param>
  211. /// <param name="text">The text to save.</param>
  212. /// <param name="encoding">The encoding.</param>
  213. public static void SaveToFile(string fileName, string text, System.Text.Encoding encoding)
  214. {
  215. fileSupport.SaveToFile(fileName, text, encoding);
  216. }
  217. /// <summary>
  218. /// Saves a text string to a file.
  219. /// </summary>
  220. /// <param name="fileName">The path and file name.</param>
  221. /// <param name="lines">The lines to save.</param>
  222. /// <param name="encoding">The encoding.</param>
  223. public static void SaveToFile(string fileName, string[] lines, System.Text.Encoding encoding)
  224. {
  225. fileSupport.SaveToFile(fileName, lines, encoding);
  226. }
  227. /// <summary>
  228. /// Loads the specified XML file into the XmlDocument.
  229. /// </summary>
  230. /// <param name="xmlDocument">The XmlDocument to load.</param>
  231. /// <param name="fileName">The path and file name.</param>
  232. public static void LoadXml(XmlDocument xmlDocument, string fileName)
  233. {
  234. fileSupport.LoadXml(xmlDocument, fileName);
  235. }
  236. /// <summary>
  237. /// Loads the specified XML file into the XmlDocument.
  238. /// </summary>
  239. /// <param name="xmlDocument">The XmlDocument to load.</param>
  240. /// <param name="fileName">The path and file name.</param>
  241. /// <param name="encoding">The encoding.</param>
  242. public static void LoadXml(XmlDocument xmlDocument, string fileName, System.Text.Encoding encoding)
  243. {
  244. fileSupport.LoadXml(xmlDocument, fileName, encoding);
  245. }
  246. #endregion
  247. /// <summary>
  248. /// Set creation time on a file.
  249. /// </summary>
  250. /// <param name="fileName"></param>
  251. public static DateTime GetCreationTime(string fileName)
  252. {
  253. return fileSupport.GetCreationTime(fileName);
  254. }
  255. /// <summary>
  256. /// Set creation time on a file.
  257. /// </summary>
  258. /// <param name="fileName"></param>
  259. /// <param name="dateTime"></param>
  260. public static void SetCreationTime(string fileName, DateTime dateTime)
  261. {
  262. fileSupport.SetCreationTime(fileName, dateTime);
  263. }
  264. /// <summary>
  265. /// Get last access time on the file.
  266. /// </summary>
  267. /// <param name="fileName"></param>
  268. public static DateTime GetLastAccessTime(string fileName)
  269. {
  270. return fileSupport.GetLastAccessTime(fileName);
  271. }
  272. /// <summary>
  273. /// Set last access time on a file.
  274. /// </summary>
  275. /// <param name="fileName"></param>
  276. /// <param name="dateTime"></param>
  277. public static void SetLastAccessTime(string fileName, DateTime dateTime)
  278. {
  279. fileSupport.SetLastAccessTime(fileName, dateTime);
  280. }
  281. /// <summary>
  282. /// Get last write time on the file.
  283. /// </summary>
  284. /// <param name="fileName"></param>
  285. public static DateTime GetLastWriteTime(string fileName)
  286. {
  287. return fileSupport.GetLastWriteTime(fileName);
  288. }
  289. /// <summary>
  290. /// Set last write time on the file.
  291. /// </summary>
  292. /// <param name="fileName"></param>
  293. /// <param name="dateTime"></param>
  294. public static void SetLastWriteTime(string fileName, DateTime dateTime)
  295. {
  296. fileSupport.SetLastWriteTime(fileName, dateTime);
  297. }
  298. /// <summary>
  299. /// Checks if the specified file exists.
  300. /// </summary>
  301. /// <param name="fileName"></param>
  302. /// <returns></returns>
  303. public static bool FileExists(string fileName)
  304. {
  305. return fileSupport.FileExists(fileName);
  306. }
  307. /// <summary>
  308. /// Returns the names of files in the specified directory that match the specified search pattern.
  309. /// </summary>
  310. /// <param name="path">The directory to search.</param>
  311. /// <param name="searchPattern">The search string to match against the names of files in path. The parameter cannot end in two periods ("..") or contain two periods ("..") followed by System.IO.Path.DirectorySeparatorChar or System.IO.Path.AltDirectorySeparatorChar, nor can it contain any of the characters in System.IO.Path.InvalidPathChars.</param>
  312. /// <returns></returns>
  313. public static string[] GetFiles(string path, string searchPattern)
  314. {
  315. return fileSupport.GetFiles(path, searchPattern);
  316. }
  317. /// <summary>
  318. /// Checks if directory exists
  319. /// </summary>
  320. /// <param name="path"></param>
  321. /// <returns></returns>
  322. public static bool DirectoryExists(string path)
  323. {
  324. return fileSupport.DirectoryExists(path);
  325. }
  326. /// <summary>
  327. /// Ensures the directory exists by trying to create it if it does not exist already.
  328. /// </summary>
  329. /// <param name="path"></param>
  330. /// <returns></returns>
  331. public static bool EnsureDirectoryExists(string path)
  332. {
  333. return fileSupport.EnsureDirectoryExists(path);
  334. }
  335. /// <summary>
  336. /// Checks if the filename is valid.
  337. /// </summary>
  338. /// <param name="fileName"></param>
  339. /// <returns>True if the filename is valid otherwise false.</returns>
  340. public static bool IsValidFileName(string fileName)
  341. {
  342. return fileSupport.IsValidFileName(fileName);
  343. }
  344. /// <summary>
  345. /// Checks if the filepath is valid
  346. /// </summary>
  347. /// <param name="pathName"></param>
  348. /// <returns>True if the filepath is valid otherwise false.</returns>
  349. public static bool IsValidPathName(string pathName)
  350. {
  351. return fileSupport.IsValidPathName(pathName);
  352. }
  353. }
  354. }