Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

C#
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);
   }
}
 
Share this answer
 
v3
Comments
Richard C Bishop 7-Jun-12 15:08pm    
Thank you for that info, but when I add that to my global.asax it does not recognize the DirectoryInfo. Do I need a using directive or something?
Sandeep Mewara 7-Jun-12 15:51pm    
Yes. Use Namespace: System.IO
Richard C Bishop 7-Jun-12 15:48pm    
Ok, I solved that issue, now I just have a question about the session["sFileName"]. Is that a session variable with the file path I set in my code-behind?
Richard C Bishop 14-Jun-12 16:01pm    
I cannot get this to work, does anyone else have an idea on how to do this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900