简体   繁体   English

如何使用Asp.net删除临时文件

[英]How to delete temporary files using Asp.net

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? 在我的应用程序中,我有一些需要经常查看的报告。不同用户多次查看报告后,显示加载错误。对于不同的系统,创建了许多临时文件。我需要在单个系统中删除这些文件现在,我手动删除temp目录中的所有临时文件并再次配置IIS。然后正确加载报告。但是我们需要频繁删除这些临时文件,这使我们的生活变得很糟糕。仅需要删除报告文件。我可以使用代码自动删除这些临时文件吗?

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

Create a thread from the Application_Start() (or write a standalone exe) 从Application_Start()创建线程(或编写独立的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 . 您可以使用一个更简单的程序,以较早的修改时间(每小时一次)删除这些文件。

Assuming you are presenting these temporary files to the user can I suggest creating an HTTP handler. 假设您正在向用户展示这些临时文件,我建议您创建一个HTTP处理程序。 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.) 示例:(对不起,C#,但是您可以从这里开始。)

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);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM