Sponsored Ad

Wednesday, January 6, 2010

How to Purge Files From a Given Directory Using c#

This method purge all files in current execution path, which are older than  10 days.

This method usage the another user defined method to get the execution path. This method delete all the files from the execution path directory which are older than the given days. you can change the number of days and also hard code the execution path.

Please let us know if you face any problem in implementing the given scenario.

Please note that this method will delete only *.log files from the given directory, you can change the file type from the code.

Code Snippet
  1. /// <summary>
  2.     /// This method purge all files in current execution path, which are older than  DaysToKeepHistory.
  3.     /// </summary>
  4.     public static void PurgeOldFiles()
  5.     {
  6.         int intNoOfDays;
  7.         DateTime TodaysDate;
  8.         DirectoryInfo dir = null;
  9.         FileInfo[] Files = null;
  10.        
  11.         try
  12.         {
  13.             intNoOfDays = 10;
  14.             TodaysDate = DateTime.Now.Date;
  15.  
  16.             dir = new DirectoryInfo(GetExecutionPath());
  17.             Files = dir.GetFiles("*.log");
  18.  
  19.             foreach (FileInfo LogF in Files)
  20.             {
  21.                 if (DateTime.Compare(TodaysDate, LogF.LastWriteTime.AddDays(intNoOfDays)) >= 0)
  22.                 {
  23.                     LogF.Delete();
  24.  
  25.                 }
  26.             }
  27.  
  28.         }
  29.         catch (Exception ex)
  30.         {
  31.            // Unable to purge Files : Message :  ex.Message
  32.             
  33.         }
  34.         finally
  35.         {
  36.             if (dir != null)
  37.             {
  38.                 dir = null;
  39.             }
  40.             if (Files != null)
  41.             {
  42.                 Files = null;
  43.             }
  44.         }
  45.  
  46.     }
 

 

Code Snippet
  1. /// <summary>
  2.     /// This method will retunrns the execution path of our application.
  3.     /// This method returns execution path for web application as well as windows service.
  4.     /// </summary>
  5.     /// <returns>Returns <see cref="System.String"/> value.</returns>
  6.     public static string GetExecutionPath()
  7.     {
  8.         string ExecutionPath = "";
  9.         try
  10.         {
  11.             ExecutionPath = System.Web.HttpContext.Current.Server.MapPath("ServiceConfig.xml");
  12.         }
  13.         catch
  14.         {
  15.         }
  16.         if (ExecutionPath == "")
  17.         {
  18.             ExecutionPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
  19.         }
  20.         try
  21.         {
  22.             string strExeName = FileSystem.Dir(ExecutionPath, 0);
  23.             string FilePath = Path.GetFullPath(ExecutionPath.Substring(0, (ExecutionPath.Length - strExeName.Length)));
  24.  
  25.             return FilePath;
  26.         }
  27.         catch
  28.         {
  29.             throw;
  30.         }
  31.     }

 

While “ServiceConfig.xml” is a configuration file kept in root of web application and windows application.

0 comments:

Post a Comment

Sponsored Ad



More Related Articles

Website Update

Recent Posts

Followers