Sample of a Manager with Singleton for make a few specific IO operation using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace Framework { //////////////////////////////////////////////////////////////////////////////////////////////////// /// Manager i/o. //////////////////////////////////////////////////////////////////////////////////////////////////// public class ManagerIO { #region singleton /// The instance. private static ManagerIO instance = null; /// The padlock. private static readonly object padlock = new object(); //////////////////////////////////////////////////////////////////////////////////////////////////// /// /// Prevents a default instance of the class from being created. /// //////////////////////////////////////////////////////////////////////////////////////////////////// ManagerIO() { //if (_Log.IsDebugEnabled) // _Log.DebugFormat("Nouvelle instance de {0}", Assembly.GetExecutingAssembly()); } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Gets the instance. /// /// The instance. //////////////////////////////////////////////////////////////////////////////////////////////////// public static ManagerIO Instance { get { lock (padlock) { if (instance == null) { instance = new ManagerIO(); } return instance; } } } #endregion Gestion du singleton //////////////////////////////////////////////////////////////////////////////////////////////////// /// Copies the directory (with protected files). /// /// The source path. /// The destination path. /// if set to true [recursive]. /// /// true if it succeeds, false if it fails. //////////////////////////////////////////////////////////////////////////////////////////////////// public Boolean CopyDirectory(string SourcePath, string DestinationPath, bool Recursive) { List Paths = new List(); List Files = new List(); Boolean bResult = false; if (String.IsNullOrEmpty(SourcePath)) throw new ArgumentNullException("SourcePath"); if (String.IsNullOrEmpty(DestinationPath)) throw new ArgumentNullException("DestinationPath"); try { foreach (string StringPath in this.DirectoryList(SourcePath, Recursive, null)) { Paths.Add(StringPath); } foreach (string StringPath in this.FileList(SourcePath, Recursive, null)) { Files.Add(StringPath); } foreach (string dirPath in Paths) { System.IO.Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath)); } foreach (string newPath in Files) { System.IO.File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), Recursive); } bResult = true; } catch (Exception ex) { ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex); bResult = false; } return bResult; } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Renames the specified file. /// /// Thrown when one or more required arguments are /// null. /// Thrown when an IO failure occurred. /// /// Full path of file to rename. /// New file name. //////////////////////////////////////////////////////////////////////////////////////////////////// public void RenameFile(string oldPath, string newName) { if (String.IsNullOrEmpty(oldPath)) throw new ArgumentNullException("oldPath"); if (String.IsNullOrEmpty(newName)) throw new ArgumentNullException("newName"); string oldName = Path.GetFileName(oldPath); // if the file name is changed if (!String.Equals(oldName, newName, StringComparison.CurrentCulture)) { string folder = Path.GetDirectoryName(oldPath); string newPath = Path.Combine(folder, newName); bool changeCase = String.Equals(oldName, newName, StringComparison.CurrentCultureIgnoreCase); // if renamed file already exists and not just changing case if (File.Exists(newPath) && !changeCase) { throw new IOException(String.Format("File already exists:n{0}", newPath)); } else if (changeCase) { // Move fails when changing case, so need to perform two moves string tempPath = Path.Combine(folder, Guid.NewGuid().ToString()); Directory.Move(oldPath, tempPath); Directory.Move(tempPath, newPath); } else { Directory.Move(oldPath, newPath); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Liste des fichier protégé ou non d'un répertoires. /// /// The root directory. /// if set to true [search all directories]. /// The filter. /// /// . //////////////////////////////////////////////////////////////////////////////////////////////////// public List FileList(string RootDirectory, bool SearchAllDirectories, Predicate Filter) { List retList = new List(); if (String.IsNullOrEmpty(RootDirectory)) throw new ArgumentNullException("RootDirectory"); try { List DirList = new List { RootDirectory }; if (SearchAllDirectories) { DirList.AddRange(this.DirectoryList(RootDirectory, SearchAllDirectories, Filter)); } foreach (string DirectoryStr in DirList) { DirectoryInfo di = new DirectoryInfo(DirectoryStr); try { foreach (FileInfo FileStr in di.GetFiles()) { try { if ((Filter == null) || (Filter(FileStr.FullName))) { retList.Add(FileStr.FullName); } } catch (Exception ax) { ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ax); } } } catch (UnauthorizedAccessException) { } catch (Exception ex) { ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex); } } } catch (Exception ex) { ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex); } return retList; } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Liste d'un répertoire protegé ou non. /// /// The root directory. /// if set to true [search all directories]. /// The filter. /// /// . //////////////////////////////////////////////////////////////////////////////////////////////////// public List DirectoryList(string RootDirectory, bool SearchAllDirectories, Predicate Filter) { List retList = new List(); if (String.IsNullOrEmpty(RootDirectory)) throw new ArgumentNullException("RootDirectory"); try { DirectoryInfo di = new DirectoryInfo(RootDirectory); foreach (DirectoryInfo DirectoryStr in di.GetDirectories()) { try { if ((Filter == null) || (Filter(DirectoryStr.FullName))) { retList.Add(DirectoryStr.FullName); if (SearchAllDirectories) { retList.AddRange(this.DirectoryList(DirectoryStr.FullName, SearchAllDirectories, Filter)); } } } catch (UnauthorizedAccessException) { } catch (Exception exx) { ManagerLogger.Instance.LogException("RemoteSurvey.Tools", exx); } } } catch (Exception ex) { ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex); } return retList; } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Gets file as stream. /// /// Thrown when one or more required arguments are /// null. /// /// Full pathname of the ps file. /// /// The file as stream. //////////////////////////////////////////////////////////////////////////////////////////////////// public byte[] GetFileAsStream(String psFilePath) { if (String.IsNullOrEmpty(psFilePath)) throw new ArgumentNullException("psFilePath"); byte[] fileContent = null; try { if (File.Exists(psFilePath)) { fileContent = File.ReadAllBytes(psFilePath); } } catch (Exception ex) { ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex); } return fileContent; } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Saves a stream as file. /// /// Array of bytes. /// The ps save file as. /// /// true if it succeeds, false if it fails. //////////////////////////////////////////////////////////////////////////////////////////////////// public Boolean SaveStreamAsFile( byte[] pByteArray, String psSaveFileAs) { Boolean bResult = false; if (String.IsNullOrEmpty(psSaveFileAs)) throw new ArgumentNullException("psSaveFileAs"); try { // instance a memory stream and pass the // byte array to its constructor MemoryStream ms = new MemoryStream(pByteArray); // instance a filestream pointing to the // storage folder, use the original file name // to name the resulting file FileStream fs = new FileStream(psSaveFileAs, FileMode.Create); // write the memory stream containing the original // file as a byte array to the filestream ms.WriteTo(fs); // clean up ms.Close(); fs.Close(); fs.Dispose(); bResult = true; } catch (Exception ex) { bResult = false; ManagerLogger.Instance.LogException("RemoteSurvey.Tools", ex); } return bResult; } } }