如何从global.asax中的Session_End事件中删除文件? [英] How can you delete a file from the Session_End event in the global.asax?
问题描述
我可能已经完全脱离基础了,但是我需要使用global.asax中的Session_End事件来删除一些文件.
我已经在web.config中将sessionState模式设置为"InProc",并做了必要的操作以触发事件.我只是无法弄清楚使用什么代码(如果可能的话)从我创建文件的位置删除文件.它们只需要是临时的,我既不希望占用磁盘空间,也不希望有一个凌乱的文件夹.
我将不胜感激任何建议和/或示例.谢谢.
I may be completely off base, but I am needing to use the Session_End event in the global.asax to delete some files.
I have set the sessionState mode to "InProc" in the web.config and done the necessary things to have the event fire. I just cannot figure out what code to use, if possible, to delete the files from the location in which I created them. They only need to be temporary and I don''t want disk space to be taken up nor have a messy folder.
I would appreciate any advice and/or examples. Thank you.
推荐答案
protected void Session_End(Object sender, EventArgs e)
{
DirectoryInfo d = new DirectoryInfo(session["sFileName"].ToString());
foreach (FileInfo f in d.GetFiles())
{
if (f.CreationTime.AddHours(1) < DateTime.Now)
File.Delete(f.FullName);
}
}
这篇关于如何从global.asax中的Session_End事件中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!