ASP.NET - delete temp files using asp.net - Asked By sasi rekha on 15-Nov-12 02:38 AM

HI,

I have created several temporary files in my asp.net app, and I need to delete them when user is done with using of those files.
Where to write the code to delete those files

Thanks in advance
Ajay Paritala replied to sasi rekha on 15-Nov-12 03:31 PM

Hi, You can create a seperate project fpr that; because you just only need to delete the files after the use.
SO create Console application or Windows app.
If you want you can create a job for this application to run the task daily in specific time.

Here is the Example i did. I used LinQ Concept to delete temp files based on name and also based of file creation date

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Mail;

namespace TaskSchedulerToDeleteTempFiles
{
    public class GenericsToDelete
    {
      public int count;

      static void Main(string[] args)
      {
        int count = 0;      
       string Path= @"C:\Users\Ajay_Paritala\Desktop\aparitala\deletebackup\backup4";

        clsCommunicatin CommObj = new clsCommunicatin();

        using (CommObj)
        {
          try
          {
            var query = from o in Directory.GetFiles(Path, "*_*_*_*_*.*", SearchOption.AllDirectories)
                  let FileName = new FileInfo(o)
                  where (FileName.Name.StartsWith(@"Bayonne") || FileName.Name.StartsWith(@"StJoseph") || FileName.Name.StartsWith(@"BronxLeb-MLK") || FileName.Name.StartsWith(@"MtSinai") || FileName.Name.StartsWith(@"BronxLeb-ED") || FileName.Name.StartsWith(@"KJMC") || FileName.Name.StartsWith(@"KJMC-ED") || FileName.Name.StartsWith(@"Hoboken") || FileName.Name.StartsWith(@"Christ") || FileName.Name.StartsWith(@"NYP"))
                 && FileName.CreationTime.Date <= DateTime.Now.AddDays(-1)
                  select FileName.FullName;

            foreach (var item in query)
            {
              count++;
              File.Delete(item);
            }
            //CommObj.sendMail(count + " Files get deleted on " + System.DateTime.Now);
            Console.WriteLine("{0} Files get deleted on {1}", count, System.DateTime.Now);
            Console.ReadLine();
          }
          catch (Exception ex)
          {
            CommObj.sendMail(ex.Message.ToString());
          }
        }
      }
    }

    public class clsCommunicatin: IDisposable
    {
      public clsCommunicatin()
      {
        // Default Constructor
      }
     
      delegate void delegateSendMail(string mbody);
      delegateSendMail sendmail = delegate(string body)
                    {
                      try
                      {
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("mail.india.com");

                        mail.From = new MailAddress("aparitala@india.com");
                        mail.To.Add("aparitala@india.com");
                        mail.Subject = "Automatic File Deletion process Status";
                        mail.Body = body;
                        SmtpServer.Send(mail);
                      }
                      catch (Exception ex)
                      {
                        ex.Message.ToString();
                      }
                    };

      public void sendMail(string mBody)
      {
        sendmail(mBody);
      }
    }
}