2

I create several temporary files in my asp.net app, and I need to delete them when the user is done using them.

I tried

 void Session_End(object sender, EventArgs e) 
    {

        System.IO.File.Delete(@"C:\Temp\test.doc");        
    }

but it's not working.

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Is the session never ending? Or is the filepath incorrect? – Paul Zahra Nov 14 '12 at 12:06
  • 1
    The file isn't being deleted. I tried both closing the browser and waiting for more than 20mins –  Nov 14 '12 at 12:08
  • Be aware that the Session_End isn't completely reliable - fx if the server crashes, the session_end will not get called... – Thomas Nov 14 '12 at 12:14

3 Answers3

2

you must grant permissions to iisuser at this folder

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
2

1) Set mode variable on web.config

<sessionState 
        mode="InProc"
        stateConnectionString="tcpip=127.0.0.1:42424"
        sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
        cookieless="false" 
        timeout="20" 
/>

2) Start a Session variable. 3) Put some code on Session_End and then check if that code is executed when the user click [X] on the browser.

Taken from the following

http://www.tek-tips.com/viewthread.cfm?qid=742667

Hope this helps

Thangamani Palanisamy
  • 5,152
  • 4
  • 32
  • 39
2

There is no good reliable (works 100% of the time) way to determine when a user leaves a web application. While conventional means will work when all is running as expected, there are edge cases that can happen that will orphan data, e.g.

The user's computer freezes or crashes... The server freezes or crashes... The user's internet connection fails... The server's internet connection fails...

You get the idea. For this reason, I personally prefer to do all processing in memory instead of writing temporary files to the web server's hdd. That way the .Net garbage collector will handle cleaning up any orphaned data.

If, however, you are processing data in sufficiently large quantities to make keeping it in memory not a viable option, set up a Windows Service or scheduled task to perform cleanup once enough time has passed to ensure that the files are stale. Continue to attempt cleanup at runtime, but the alternate method should allow you to handle any data which becomes orphaned over time.

Kevin
  • 704
  • 3
  • 4