2

我在我的 asp.net 应用程序中创建了几个临时文件,当用户完成使用它们时我需要删除它们。

我试过了

 void Session_End(object sender, EventArgs e) 
    {

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

但它不起作用。

有任何想法吗?

4

3 回答 3

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.

于 2012-11-14T13:11:46.210 回答
2

您必须在此文件夹中向 iisuser 授予权限

于 2012-11-14T12:03:38.087 回答
2

1) 在 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)启动一个会话变量。3) 将一些代码放在 Session_End 上,然后检查当用户在浏览器上单击 [X] 时是否执行了该代码。

取自以下

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

希望这可以帮助

于 2012-11-14T12:07:49.370 回答