c# – 如何使用和范围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);
}
}
我对global.asax的实际工作方式感到困惑.它是否像界面一样使用?
解决方法:
查看如何使用Global.asax的好地方是阅读ASP.NET Quickstart on it’s usage.每个Web应用程序/站点都有一个.这有点像全球级别的事件.
请注意,在大多数服务器上,Application_End事件不会经常触发.只有在卸载/回收IIS应用程序池,修改了web.config,在/ Bin中更改了程序集或Web服务器停止的其他情况时,才会触发它.在一个受欢迎的网站上,可能需要几周,几个月才会发生事件.
标签:c,file,asp-net,global-asax 来源: https://codeday.me/bug/20190522/1151706.html