tags:

views:

280

answers:

2

In my application, I have some reports which needs to be viewed frequently.After viewing the reports many times by different users, it shows load error.For different systems, many temporary files are created.i need to delete those files in my single system.now i manually deleting all the temporary files in the temp directory and configure the IIS again.then the report loads properly.But we need to delete these temporary files frequently which makes our life dreadful.Only the report files needs to be deleted.How can i delete these temporary files automatically using code?

I have used the following code for this.but some files cant be deleted as those files are in use.Do those temporary files in other system can cause load error in our system?how can i solve this?

dim temp as string=Environment.GetEnvironmentVariable("TEMP")
dim k as sting()=System.IO.Directory.GetFiles(temp)
dim i as integer

For i=0 to k.length
    On Error Resume Next
    If k(i).Contains(".rpt") then
        kill(k(i))
        System.IO.File.Delete(k(i))
Next
A: 

Assuming you are presenting these temporary files to the user can I suggest creating an HTTP handler. The handler will provide the ability to generate a file and deliver it to the user to be either downloaded or viewed in browser. This approach allows for the customization of caching.

The example below is only showing the handler portion, this is as basic as it gets as doesn't go into the file creation as I am not sure how you are creating the files currently. You can send a stream of some sort.

Example: (Sorry in C#, but you can go from here.)

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

namespace Handlers
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://www.tempuri.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ColorImage : IHttpHandler
    {
        public bool IsReusable { get { return true; } }

        public void ProcessRequest(HttpContext context)
        {
            Bitmap bmGenerate = CreateBitmapMethod();
            context.Response.ContentType = "image/png";
            context.Response.AddHeader("Response-Type", "image/png");

            using (MemoryStream memoryStream = new MemoryStream())
            {
                bmGenerate.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                memoryStream.WriteTo(context.Response.OutputStream);
            }
        }
    }
}
Dustin Laine
how can i do this?can you send me the code?
Nandini
edited answer to include sample
Dustin Laine
@durilai,Your answer is not correct
Nandini
Sorry to hear that, not sure what would not be correct. I have used similar solution in several applications. I would not rely on my code above, but it should give you a start. I stand by my recommendation of using an HTTP handler.
Dustin Laine
+1  A: 

Create a thread from the Application_Start() (or write a standalone exe)

You can just automate what you are manually doing. You can delete these files with a older modifier day, once an hour, with a very simple program .

Dennis Cheung
I agree you can have a separate application thread running on the application start that periodically checks your folder and clears it. The only challenge is if your deletion routine tries to delete a file that is currently being accessed. You can reduce that problem by looking at the time-stamp and purging older files that you are sure your users are not accessing.
Nikhil