Where is best to delete temporary files in an ASP.net application?

1.1k Views Asked by At

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?

3

There are 3 best solutions below

0
burning_LEGION On

you must grant permissions to iisuser at this folder

0
Thangamani  Palanisamy On

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

0
Kevin On

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.