FileSupport.cs 14 KB

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