[.NET)[C#]Sample of a Manager with Singleton for make a few specific IO operation


SUBMITTED BY: Guest

DATE: Dec. 7, 2013, 4:19 p.m.

FORMAT: Text only

SIZE: 15.4 kB

HITS: 54501

  1. Sample of a Manager with Singleton for make a few specific IO operation
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace Framework
  7. {
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /// <summary> Manager i/o. </summary>
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////
  11. public class ManagerIO
  12. {
  13. #region singleton
  14. /// <summary> The instance. </summary>
  15. private static ManagerIO instance = null;
  16. /// <summary> The padlock. </summary>
  17. private static readonly object padlock = new object();
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. /// <summary>
  20. /// Prevents a default instance of the <see cref="ManagerIO"/> class from being created.
  21. /// </summary>
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////
  23. ManagerIO()
  24. {
  25. //if (_Log.IsDebugEnabled)
  26. // _Log.DebugFormat("Nouvelle instance de {0}", Assembly.GetExecutingAssembly());
  27. }
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////
  29. /// <summary> Gets the instance. </summary>
  30. ///
  31. /// <value> The instance. </value>
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////
  33. public static ManagerIO Instance
  34. {
  35. get
  36. {
  37. lock (padlock)
  38. {
  39. if (instance == null)
  40. {
  41. instance = new ManagerIO();
  42. }
  43. return instance;
  44. }
  45. }
  46. }
  47. #endregion Gestion du singleton
  48. ////////////////////////////////////////////////////////////////////////////////////////////////////
  49. /// <summary> Copies the directory (with protected files). </summary>
  50. ///
  51. /// <param name="SourcePath"> The source path. </param>
  52. /// <param name="DestinationPath"> The destination path. </param>
  53. /// <param name="Recursive"> if set to <c>true</c> [recursive]. </param>
  54. ///
  55. /// <returns> true if it succeeds, false if it fails. </returns>
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////
  57. public Boolean CopyDirectory(string SourcePath, string DestinationPath, bool Recursive)
  58. {
  59. List<string> Paths = new List<string>();
  60. List<string> Files = new List<string>();
  61. Boolean bResult = false;
  62. if (String.IsNullOrEmpty(SourcePath))
  63. throw new ArgumentNullException("SourcePath");
  64. if (String.IsNullOrEmpty(DestinationPath))
  65. throw new ArgumentNullException("DestinationPath");
  66. try
  67. {
  68. foreach (string StringPath in this.DirectoryList(SourcePath, Recursive, null))
  69. {
  70. Paths.Add(StringPath);
  71. }
  72. foreach (string StringPath in this.FileList(SourcePath, Recursive, null))
  73. {
  74. Files.Add(StringPath);
  75. }
  76. foreach (string dirPath in Paths)
  77. {
  78. System.IO.Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
  79. }
  80. foreach (string newPath in Files)
  81. {
  82. System.IO.File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), Recursive);
  83. }
  84. bResult = true;
  85. }
  86. catch (Exception ex)
  87. {
  88. ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex);
  89. bResult = false;
  90. }
  91. return bResult;
  92. }
  93. ////////////////////////////////////////////////////////////////////////////////////////////////////
  94. /// <summary> Renames the specified file. </summary>
  95. ///
  96. /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are
  97. /// null. </exception>
  98. /// <exception cref="IOException"> Thrown when an IO failure occurred. </exception>
  99. ///
  100. /// <param name="oldPath"> Full path of file to rename. </param>
  101. /// <param name="newName"> New file name. </param>
  102. ////////////////////////////////////////////////////////////////////////////////////////////////////
  103. public void RenameFile(string oldPath, string newName)
  104. {
  105. if (String.IsNullOrEmpty(oldPath))
  106. throw new ArgumentNullException("oldPath");
  107. if (String.IsNullOrEmpty(newName))
  108. throw new ArgumentNullException("newName");
  109. string oldName = Path.GetFileName(oldPath);
  110. // if the file name is changed
  111. if (!String.Equals(oldName, newName, StringComparison.CurrentCulture))
  112. {
  113. string folder = Path.GetDirectoryName(oldPath);
  114. string newPath = Path.Combine(folder, newName);
  115. bool changeCase = String.Equals(oldName, newName, StringComparison.CurrentCultureIgnoreCase);
  116. // if renamed file already exists and not just changing case
  117. if (File.Exists(newPath) && !changeCase)
  118. {
  119. throw new IOException(String.Format("File already exists:n{0}", newPath));
  120. }
  121. else if (changeCase)
  122. {
  123. // Move fails when changing case, so need to perform two moves
  124. string tempPath = Path.Combine(folder, Guid.NewGuid().ToString());
  125. Directory.Move(oldPath, tempPath);
  126. Directory.Move(tempPath, newPath);
  127. }
  128. else
  129. {
  130. Directory.Move(oldPath, newPath);
  131. }
  132. }
  133. }
  134. ////////////////////////////////////////////////////////////////////////////////////////////////////
  135. /// <summary> Liste des fichier protégé ou non d'un répertoires. </summary>
  136. ///
  137. /// <param name="RootDirectory"> The root directory. </param>
  138. /// <param name="SearchAllDirectories"> if set to <c>true</c> [search all directories]. </param>
  139. /// <param name="Filter"> The filter. </param>
  140. ///
  141. /// <returns> . </returns>
  142. ////////////////////////////////////////////////////////////////////////////////////////////////////
  143. public List<string> FileList(string RootDirectory,
  144. bool SearchAllDirectories, Predicate<string> Filter)
  145. {
  146. List<string> retList = new List<string>();
  147. if (String.IsNullOrEmpty(RootDirectory))
  148. throw new ArgumentNullException("RootDirectory");
  149. try
  150. {
  151. List<string> DirList = new List<string> { RootDirectory };
  152. if (SearchAllDirectories)
  153. {
  154. DirList.AddRange(this.DirectoryList(RootDirectory, SearchAllDirectories, Filter));
  155. }
  156. foreach (string DirectoryStr in DirList)
  157. {
  158. DirectoryInfo di = new DirectoryInfo(DirectoryStr);
  159. try
  160. {
  161. foreach (FileInfo FileStr in di.GetFiles())
  162. {
  163. try
  164. {
  165. if ((Filter == null) || (Filter(FileStr.FullName)))
  166. {
  167. retList.Add(FileStr.FullName);
  168. }
  169. }
  170. catch (Exception ax)
  171. {
  172. ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ax);
  173. }
  174. }
  175. }
  176. catch (UnauthorizedAccessException)
  177. {
  178. }
  179. catch (Exception ex)
  180. {
  181. ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex);
  182. }
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex);
  188. }
  189. return retList;
  190. }
  191. ////////////////////////////////////////////////////////////////////////////////////////////////////
  192. /// <summary> Liste d'un répertoire protegé ou non. </summary>
  193. ///
  194. /// <param name="RootDirectory"> The root directory. </param>
  195. /// <param name="SearchAllDirectories"> if set to <c>true</c> [search all directories]. </param>
  196. /// <param name="Filter"> The filter. </param>
  197. ///
  198. /// <returns> . </returns>
  199. ////////////////////////////////////////////////////////////////////////////////////////////////////
  200. public List<string> DirectoryList(string RootDirectory,
  201. bool SearchAllDirectories, Predicate<string> Filter)
  202. {
  203. List<string> retList = new List<string>();
  204. if (String.IsNullOrEmpty(RootDirectory))
  205. throw new ArgumentNullException("RootDirectory");
  206. try
  207. {
  208. DirectoryInfo di = new DirectoryInfo(RootDirectory);
  209. foreach (DirectoryInfo DirectoryStr in di.GetDirectories())
  210. {
  211. try
  212. {
  213. if ((Filter == null) || (Filter(DirectoryStr.FullName)))
  214. {
  215. retList.Add(DirectoryStr.FullName);
  216. if (SearchAllDirectories)
  217. {
  218. retList.AddRange(this.DirectoryList(DirectoryStr.FullName, SearchAllDirectories,
  219. Filter));
  220. }
  221. }
  222. }
  223. catch (UnauthorizedAccessException)
  224. {
  225. }
  226. catch (Exception exx)
  227. {
  228. ManagerLogger.Instance.LogException("RemoteSurvey.Tools", exx);
  229. }
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex);
  235. }
  236. return retList;
  237. }
  238. ////////////////////////////////////////////////////////////////////////////////////////////////////
  239. /// <summary> Gets file as stream. </summary>
  240. ///
  241. /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are
  242. /// null. </exception>
  243. ///
  244. /// <param name="psFilePath"> Full pathname of the ps file. </param>
  245. ///
  246. /// <returns> The file as stream. </returns>
  247. ////////////////////////////////////////////////////////////////////////////////////////////////////
  248. public byte[] GetFileAsStream(String psFilePath)
  249. {
  250. if (String.IsNullOrEmpty(psFilePath))
  251. throw new ArgumentNullException("psFilePath");
  252. byte[] fileContent = null;
  253. try
  254. {
  255. if (File.Exists(psFilePath))
  256. {
  257. fileContent = File.ReadAllBytes(psFilePath);
  258. }
  259. }
  260. catch (Exception ex)
  261. {
  262. ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex);
  263. }
  264. return fileContent;
  265. }
  266. ////////////////////////////////////////////////////////////////////////////////////////////////////
  267. /// <summary> Saves a stream as file. </summary>
  268. ///
  269. /// <param name="pByteArray"> Array of bytes. </param>
  270. /// <param name="psSaveFileAs"> The ps save file as. </param>
  271. ///
  272. /// <returns> true if it succeeds, false if it fails. </returns>
  273. ////////////////////////////////////////////////////////////////////////////////////////////////////
  274. public Boolean SaveStreamAsFile( byte[] pByteArray, String psSaveFileAs)
  275. {
  276. Boolean bResult = false;
  277. if (String.IsNullOrEmpty(psSaveFileAs))
  278. throw new ArgumentNullException("psSaveFileAs");
  279. try
  280. {
  281. // instance a memory stream and pass the
  282. // byte array to its constructor
  283. MemoryStream ms = new MemoryStream(pByteArray);
  284. // instance a filestream pointing to the
  285. // storage folder, use the original file name
  286. // to name the resulting file
  287. FileStream fs = new FileStream(psSaveFileAs, FileMode.Create);
  288. // write the memory stream containing the original
  289. // file as a byte array to the filestream
  290. ms.WriteTo(fs);
  291. // clean up
  292. ms.Close();
  293. fs.Close();
  294. fs.Dispose();
  295. bResult = true;
  296. }
  297. catch (Exception ex)
  298. {
  299. bResult = false;
  300. ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex);
  301. }
  302. return bResult;
  303. }
  304. }
  305. }

comments powered by Disqus