andrew4582

IOHelper and Extentions classes

Feb 11th, 2012
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security;
  6.  
  7. namespace Core.IO {
  8.  
  9.     /// <summary>
  10.     /// System.IO helpers and extentions
  11.     /// </summary>
  12.     public static class IOHelper {
  13.  
  14.         /// <summary>
  15.         /// Deletes a file by first checking it's existance, then deletes if found.
  16.         /// If the delete fails, then sets the file's <seealso cref="System.IO.FileAttributes"/> to '<seealso cref="System.IO.FileAttributes.Normal"/>'
  17.         /// and attempts to delete the file again.
  18.         /// </summary>
  19.         /// <param name="path">File's full path to be deleted</param>
  20.         /// <returns>True if the file was deleted, otherwise failed to delete the file</returns>
  21.         public static bool DeleteFileSafe(string path) {
  22.  
  23.             if(string.IsNullOrWhiteSpace(path))
  24.                 throw new ArgumentNullException("File path is missing");
  25.  
  26.             try {
  27.                 FileInfo finfo = new FileInfo(path);
  28.  
  29.                 if(!finfo.Exists)
  30.                     return false;
  31.  
  32.                 try {
  33.                     finfo.Attributes = FileAttributes.Normal;
  34.                 }
  35.                 catch(IOException) {
  36.                     return false;
  37.                 }
  38.                 catch(SecurityException) {
  39.                     return false;
  40.                 }
  41.  
  42.                 //attempt to delete the file
  43.                 finfo.Delete();
  44.  
  45.                 //return true if the file doesn't exists
  46.                 return !finfo.Exists;
  47.             }
  48.             catch(IOException) {
  49.                 return false;
  50.             }
  51.             catch(SecurityException) {
  52.                 return false;
  53.             }
  54.             catch(UnauthorizedAccessException) {
  55.                 return false;
  56.             }
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Delete's all files within the provided directory
  61.         /// </summary>
  62.         /// <param name="directoryPath">Directory's full path</param>
  63.         /// <param name="pattern">The search string to match against the names of files in path</param>
  64.         /// <returns>Number of errors</returns>
  65.         public static int DeleteAllFilesSafe(string directoryPath,string pattern = "*.*") {
  66.  
  67.             int deleteErrors = 0;
  68.             List<string> subDirectories = Directory.GetDirectories(directoryPath,pattern).ToList();
  69.  
  70.             //insert the directory to the top of the list, to delete all children first.
  71.             subDirectories.Insert(0,directoryPath);
  72.  
  73.             foreach(string d in subDirectories) {
  74.  
  75.                 string[] dirfiles = Directory.GetFiles(d,pattern,SearchOption.AllDirectories);
  76.  
  77.                 foreach(string fpath in dirfiles) {
  78.                     bool success = DeleteFileSafe(fpath);
  79.                     if(!success)
  80.                         deleteErrors++;
  81.                 }
  82.             }
  83.  
  84.             return deleteErrors;
  85.         }
  86.  
  87.         /// <summary>
  88.         /// Delete's all files and directories within the provided directory
  89.         /// </summary>
  90.         /// <param name="directoryPath">Directory's full path</param>
  91.         /// <param name="pattern">The search string to match against the names of files in path</param>
  92.         /// <returns>Number of errors</returns>
  93.         public static int DeleteDirectory(string directoryPath,string pattern = "*.*") {
  94.  
  95.             int deleteErrors = DeleteAllFilesSafe(directoryPath,pattern);
  96.            
  97.             if(deleteErrors > 0) {
  98.                 //if we can't delete all the files, then we can't delete the directories
  99.                 return deleteErrors;
  100.             }
  101.  
  102.             List<string> rootDirectories = new List<string>(Directory.GetDirectories(directoryPath,pattern));
  103.            
  104.             //insert the directory to the top of the list, to delete all children first.
  105.             rootDirectories.Insert(0,directoryPath);
  106.              
  107.             foreach(string d in rootDirectories) {
  108.                 try {
  109.                     if(Directory.Exists(d))
  110.                         Directory.Delete(d,true);
  111.                 }
  112.                 catch {
  113.                     deleteErrors++;
  114.                 }
  115.             }
  116.             return deleteErrors;
  117.         }
  118.  
  119.     }
  120.  
  121.     public static class IOExtentions {
  122.  
  123.         public static bool DeleteSafe(this FileInfo finfo) {
  124.             return IOHelper.DeleteFileSafe(finfo.FullName);
  125.         }
  126.  
  127.         public static int DeleteAllFilesSafe(this DirectoryInfo dinfo) {
  128.             return IOHelper.DeleteAllFilesSafe(dinfo.FullName);
  129.         }
  130.  
  131.         public static int DeleteDirectory(this DirectoryInfo dinfo) {
  132.             return IOHelper.DeleteDirectory(dinfo.FullName);
  133.         }
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment