Thursday, 2 January 2014

File and Directory Deletion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Modal
{
    public class TaskDelete : TaskManager
    {

        #region PROPERTIES

        //public string Path { get; set; }
        //public string Extension { get; set; }
        //public int DaysCount { get; set; }
        //public bool Recursive { get; set; }

        #endregion

        private System.IO.DirectoryInfo obvDirectory = null;
        private System.IO.FileInfo[] obvFile = null;

        #region CONSTRUCTOR
        /// <summary>
        /// Initializes a new instance of the TaskDelete class on the specified path.
        /// </summary>
        /// <param name="path"> A string specifying the path on which to create the DirectoryInfo</param>
        private TaskDelete()
        {

        }
        public TaskDelete(string path)
        {
            obvDirectory = new System.IO.DirectoryInfo(path);

        }
        #endregion



        #region METHODS

        /// <summary>
        /// Delete All files and directory.
        /// </summary>
        /// <param name="IsDeleteFiles">ture: If delete all files, otherwise false.</param>
        /// <param name="IsDeleteDirectory">ture: If delete all sub directories and files, otherwise false.</param>
        public bool Delete(bool IsDeleteFiles, bool IsDeleteDirectory)
        {
            try
            {
                if (IsDeleteDirectory && IsDeleteFiles)
                    obvDirectory.Delete(IsDeleteDirectory);
                else if (IsDeleteFiles)
                    obvDirectory.GetFiles().ToList().ForEach(f => f.Delete());
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Delete files with extesion and number of days.
        /// </summary>
        /// <param name="extension">Specific file with extension. ie : "*.txt;*.xml;*.tmp".</param>
        /// <param name="noOfDays">Days count from created date.</param>      
        /// <returns>True, If successful, otherwise false.</returns>
        public bool Delete(string extension, int noOfDays)
        {
            try
            {
                if(extension.Contains(".dll") || extension.Contains(".exe"))
                    return false ;
               
                obvFile = obvDirectory.GetFiles(extension).Where(f => DateTime.Now.Subtract(f.CreationTime).TotalDays <= noOfDays).ToArray();
                obvFile.ToList().ForEach(f => f.Delete());
                return true;
            }
            catch
            {
                return false;
            }

        }

        /// <summary>
        /// Delete all file with in specified number of days.
        /// </summary>
        /// <param name="noOfDays">Days count from created date.</param>
        /// <returns></returns>
        public bool Delete(int noOfDays)
        {
            try
            {
                obvFile = obvDirectory.GetFiles().Where(f => DateTime.Now.Subtract(f.CreationTime).TotalDays <= noOfDays).ToArray();
                obvFile.ToList().ForEach(f => f.Delete());
                return true;
            }
            catch
            {
                return false;
            }

        }

        /// <summary>
        /// Delete all file with specified extension.
        /// </summary>
        /// <param name="extension">Specific file with extension. ie : "*.txt;*.xml;*.tmp".</param>
        /// <returns></returns>
        public bool Delete(string extension)
        {
            try
            {
                if(extension.Contains(".dll") || extension.Contains(".exe"))
                    return false ;
                obvFile = obvDirectory.GetFiles(extension).ToArray();
                obvFile.ToList().ForEach(f => f.Delete());
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

    }


}

No comments:

Post a Comment