简体   繁体   English

最好在ASP.net应用程序中删除临时文件在哪里?

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

I create several temporary files in my asp.net app, and I need to delete them when the user is done using them. 我在asp.net应用程序中创建了几个临时文件,当用户使用完它们后,需要将其删除。

I tried 我试过了

 void Session_End(object sender, EventArgs e) 
    {

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

but it's not working. 但它不起作用。

Any ideas? 有任何想法吗?

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

1) Set mode variable on web.config 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) Start a Session variable. 2)启动一个Session变量。 3) Put some code on Session_End and then check if that code is executed when the user click [X] on the browser. 3)在Session_End上放置一些代码,然后在用户单击浏览器上的[X]时检查是否执行了该代码。

Taken from the following 取自以下

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

Hope this helps 希望这可以帮助

There is no good reliable (works 100% of the time) way to determine when a user leaves a web application. 没有确定用户何时离开Web应用程序的可靠方法(可以100%地起作用)。 While conventional means will work when all is running as expected, there are edge cases that can happen that will orphan data, eg 尽管常规方法将在所有组件按预期运行时都能正常工作,但可能会发生边缘情况,从而使数据孤立,例如

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. 因此,我个人更喜欢在内存中进行所有处理,而不是将临时文件写入Web服务器的HDD。 That way the .Net garbage collector will handle cleaning up any orphaned data. 这样,.Net垃圾收集器将处理清理所有孤立数据。

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. 但是,如果要处理足够大的数据以至于无法将其保存在内存中,请设置Windows服务或计划任务以在足够的时间过去后执行清理以确保文件已过时。 Continue to attempt cleanup at runtime, but the alternate method should allow you to handle any data which becomes orphaned over time. 继续尝试在运行时进行清理,但是替代方法应允许您处理随时间而变得孤立的所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM