Reputation: 393
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
Upvotes: 1
Views: 1834
Reputation: 24747
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 .
Upvotes: 1
Reputation: 38503
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);
}
}
}
}
Upvotes: 0