如何使用和范围的global.asax(用于应用程序结束后的文件清理)

Dav*_*ave 5 c# asp.net file global-asax

关于动态存储临时映像并在Web服务器的文件系统上处理它们的清理的主题:(在.NET 3.5中使用C#).

有人建议我使用一个global.asax文件来处理这个问题.

我只是无法弄清楚这件事是如何运作的.

我有两个单独的申请......

我已经发现global.asax应该在网站的根目录中.

问题:

1)如何global.asax只为这两个特定的应用程序启动.

2)两个应用程序都需要创建一个字符串列表(文件位置),然后在应用程序终止时删除它们.我是在应用程序中实例化此数组,还是在global.asax

我的代码看起来像这样:

List<string> fileLocations = new List<string>();  
//I don't know where to put this.

//The next line of code will be in both applications (this could 
//be called many times in one application session.  The names of 
//the files are generated from the clock (milliseconds and 
//seconds combined, I might change this to use the actual 
//random class combined with sessionID)
fileLocations.Add(file);

void Application_End(object sender, EventArgs e) 
{
    //  Code that runs on application shutdown
    foreach(string file in fileLocations)
    {
        if(File.Exists(file))
            File.Delete(file);
    }    
}
Run Code Online (Sandbox Code Playgroud)

我对global.asax的实际工作方式感到困惑.它是否像界面一样使用?

Dan*_*plo 3

了解如何使用 Global.asax 的一个好地方是阅读有关其用法的 ASP.NET 快速入门。每个网络应用程序/站点都有一个。这有点像全球级别的活动。

请注意,在大多数服务器上,Application_End 事件不会经常触发。仅当 IIS 应用程序池被卸载/回收、web.config 被修改、/Bin 中的程序集被更改或 Web 服务器停止的其他情况时,它才会触发。在热门网站上,您的活动可能需要几周、几个月的时间才能触发。