C#: Methods help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shani Aulakh
    New Member
    • Nov 2007
    • 44

    C#: Methods help

    Hi there,

    I have to create a class that will monitor a given folder and delete all files older that X number of days using namespace "Files_ExtL ib1"

    There are 2 methods I have to create. DeleteFilesOlde rThan() and FilesOlderThan( ) and have first method call the second one.

    I have to follow the following structure..
    Code:
    using System;
    
    namespace Files_ExtLib1
    {
    	/// <summary>
    	/// Summary description for FolderWatcher.
    	/// </summary>
    	public class FolderWatcher
    	{
            string m_FolderPath;
    
    		public FolderWatcher()
    		{
    			//
    			// TODO: Add constructor logic here
    			//
    		}
    
            #region Property
            public string FolderPath
            {
                get 
                {
                    return m_FolderPath;
                }
                set
                {
                    m_FolderPath = value;
                }
            }
            #endregion
    
    
            public int DeleteFilesOlderThan(int Days, int Hrs, int Mins)
            {
            }
    
            public string[] FilesOlderThan(int Days, int Hrs, int Mins)
            {
            }
    I personally think it would make it easier if i used a DateTime structure to represent the date and time. I think it just makes the following code easier to do.
    Code:
    public static DateTime GetRelativeDateTime(int days, int hours, int minutes) 
    { 
    return DateTime.Now.AddDays(-days).AddHours(-hours).AddMinutes(-minutes); 
    } 
    
    
    Then using the relative DateTime, you can filter out files fairly easily: 
    public static string[] FilesOlderThan(DateTime dt) 
    { 
    DirectoryInfo directory = new DirectoryInfo(Directory.GetCurrentDirectory()); 
    FileInfo[] files = directory.GetFiles(); //get FileInfos for the files in the current directory 
    ArrayList older = new ArrayList(); //list to hold the result 
    
    foreach(FileInfo file in files) 
    { 
    if(file.CreationTime < dt) //if smaller (older) than the relative time 
    older.Add(file.Name); //add it to the list 
    } 
    
    return (string[])older.ToArray(typeof(string)); //return as an array 
    }
    but i need to follow the structure...any help will be appreciated Thanks!!!
  • Shani Aulakh
    New Member
    • Nov 2007
    • 44

    #2
    I added code in the methods. Just want someone to have a look at it because i dont want to lose real data in bulks Thanks

    Code:
    using System;
    using System.IO;
    using System.Windows.Forms;
    using System.Collections;
    
    namespace Files_ExtLib1
    {
    	/// <summary>
    	/// Summary description for FolderWatcher.
    	/// </summary>
    	public class FolderWatcher
    	{
            string m_FolderPath;
    
    		public FolderWatcher()
    		{
    			//
    			// TODO: Add constructor logic here
    			//
    		}
    
            #region Property
            public string FolderPath
            {
                get 
                {
                    return m_FolderPath;
                }
                set
                {
                    m_FolderPath = value;
                }
            }
            #endregion
    
            /// <summary>
            /// Deletes the files older the days, hours, and
            /// minutes specified
            /// </summary>
            /// <param name="Days"></param>
            /// <param name="Hrs"></param>
            /// <param name="Mins"></param>
            /// <returns>Number of files deleted</returns>
            public int DeleteFilesOlderThan(int Days, int Hrs, int Mins)
            {
                string[] oldFilePaths = FilesOlderThan(Days, Hrs, Mins);
                int count = 0;
                foreach(string filePath in oldFilePaths)
                {
                     try
                     {
                        File.Delete(filePath);
                        count++;
                     }
                     catch
                     {
                        MessageBox.Show("Unable to delete " + filePath);
                     } 
                }
                // presumably number of files deleted to be returned
                return count;
    
            }
    
    
            /// <summary>
            /// Reurns filenames having last modified time older than
            /// specified period.
            /// </summary>
            /// <param name="Days"></param>
            /// <param name="Hrs"></param>
            /// <param name="Mins"></param>
            /// <returns></returns>
            public string[] FilesOlderThan(int Days, int Hrs, int Mins)
            {
                TimeSpan ts = new TimeSpan(Days, Hrs, Mins, 0);
                ArrayList oldFilePaths = new ArrayList(); 
                // FolderPath is assumed to be path being watched
                string[] filePaths = Directory.GetFiles(FolderPath); 
                DateTime currentDate = DateTime.Now;
                foreach(string filePath in filePaths)
                {
                   DateTime creationDate = File.GetCreationTime(filePath);
                   if ((currentDate - creationDate) > ts)
                   {  
                       oldFilePaths.Add(filePath);          
                   }                
               }
             
               return (string[])oldFilePaths.ToArray(typeof(string));
            }
            
        //    public static DateTime GetRelativeDateTime(int days, int hours, int minutes)
        //    {
        //        return DateTime.Now.AddDays(-days).AddHours(-hours).AddMinutes(-minutes);
        //    }
    
        //    public static string[] FilesOlderThan(DateTime dt)
        //    {
        //        DirectoryInfo directory = new DirectoryInfo(Directory.GetCurrentDirectory());
        //        FileInfo[] files = directory.GetFiles(); //get FileInfos for the files in the current directory 
        //        ArrayList older = new ArrayList(); //list to hold the result 
    
        //        foreach (FileInfo file in files)
        //        {
        //            if (file.CreationTime < dt) //if smaller (older) than the relative time 
        //                older.Add(file.Name); //add it to the list 
        //        }
    
        //        return (string[])older.ToArray(typeof(string)); //return as an array 
        //    } 
    
        }
    }

    Comment

    Working...