Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security;
- namespace Core.IO {
- /// <summary>
- /// System.IO helpers and extentions
- /// </summary>
- public static class IOHelper {
- /// <summary>
- /// Deletes a file by first checking it's existance, then deletes if found.
- /// If the delete fails, then sets the file's <seealso cref="System.IO.FileAttributes"/> to '<seealso cref="System.IO.FileAttributes.Normal"/>'
- /// and attempts to delete the file again.
- /// </summary>
- /// <param name="path">File's full path to be deleted</param>
- /// <returns>True if the file was deleted, otherwise failed to delete the file</returns>
- public static bool DeleteFileSafe(string path) {
- if(string.IsNullOrWhiteSpace(path))
- throw new ArgumentNullException("File path is missing");
- try {
- FileInfo finfo = new FileInfo(path);
- if(!finfo.Exists)
- return false;
- try {
- finfo.Attributes = FileAttributes.Normal;
- }
- catch(IOException) {
- return false;
- }
- catch(SecurityException) {
- return false;
- }
- //attempt to delete the file
- finfo.Delete();
- //return true if the file doesn't exists
- return !finfo.Exists;
- }
- catch(IOException) {
- return false;
- }
- catch(SecurityException) {
- return false;
- }
- catch(UnauthorizedAccessException) {
- return false;
- }
- }
- /// <summary>
- /// Delete's all files within the provided directory
- /// </summary>
- /// <param name="directoryPath">Directory's full path</param>
- /// <param name="pattern">The search string to match against the names of files in path</param>
- /// <returns>Number of errors</returns>
- public static int DeleteAllFilesSafe(string directoryPath,string pattern = "*.*") {
- int deleteErrors = 0;
- List<string> subDirectories = Directory.GetDirectories(directoryPath,pattern).ToList();
- //insert the directory to the top of the list, to delete all children first.
- subDirectories.Insert(0,directoryPath);
- foreach(string d in subDirectories) {
- string[] dirfiles = Directory.GetFiles(d,pattern,SearchOption.AllDirectories);
- foreach(string fpath in dirfiles) {
- bool success = DeleteFileSafe(fpath);
- if(!success)
- deleteErrors++;
- }
- }
- return deleteErrors;
- }
- /// <summary>
- /// Delete's all files and directories within the provided directory
- /// </summary>
- /// <param name="directoryPath">Directory's full path</param>
- /// <param name="pattern">The search string to match against the names of files in path</param>
- /// <returns>Number of errors</returns>
- public static int DeleteDirectory(string directoryPath,string pattern = "*.*") {
- int deleteErrors = DeleteAllFilesSafe(directoryPath,pattern);
- if(deleteErrors > 0) {
- //if we can't delete all the files, then we can't delete the directories
- return deleteErrors;
- }
- List<string> rootDirectories = new List<string>(Directory.GetDirectories(directoryPath,pattern));
- //insert the directory to the top of the list, to delete all children first.
- rootDirectories.Insert(0,directoryPath);
- foreach(string d in rootDirectories) {
- try {
- if(Directory.Exists(d))
- Directory.Delete(d,true);
- }
- catch {
- deleteErrors++;
- }
- }
- return deleteErrors;
- }
- }
- public static class IOExtentions {
- public static bool DeleteSafe(this FileInfo finfo) {
- return IOHelper.DeleteFileSafe(finfo.FullName);
- }
- public static int DeleteAllFilesSafe(this DirectoryInfo dinfo) {
- return IOHelper.DeleteAllFilesSafe(dinfo.FullName);
- }
- public static int DeleteDirectory(this DirectoryInfo dinfo) {
- return IOHelper.DeleteDirectory(dinfo.FullName);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment