如何自动清除zedgraph图像的临时目录?

时间:2012-04-20 15:00:25

标签: asp.net zedgraph

我们在我们的asp.net(vb)网络应用程序中使用zedgraph webcontrols进行制图。生成的图像的临时目录填满,现在已达到2GB +。

有没有办法自动清除这个目录,还是我必须实施一个讨厌的HttpContext.Cache expired callback黑客来删除那里的旧文件?

谢谢,Flo

1 个答案:

答案 0 :(得分:1)

因此,我选择了一个更常见的apporach来处理自己的文件数量,而不是亲自编辑和重新编译zedgraphweb http://sourceforge.net/projects/zedgraph。我正在使用Cache的CacheItemRemovedCallback在Web应用程序中执行代码,就像使用计时器一样。此代码重新填充缓存项,因此更新循环。瞧:在网络应用中每5分钟清理一次磁盘。

Public Module Maintenance
    Private Const triggerKey As String = "MaintenanceDiskCleaner"
    Friend Sub Run()
        Try
            SyncLock triggerKey
                If Hosting.HostingEnvironment.Cache(triggerKey) Is Nothing Then
                    Hosting.HostingEnvironment.Cache.Add(triggerKey, _
                                                  DateTime.Now, _
                                                  Nothing, _
                                                  DateTime.Now.AddMinutes(5), _
                                                  Cache.NoSlidingExpiration, _
                                                  CacheItemPriority.Default, _
                                                  AddressOf CacheItemRemovedCallback)
                End If
            End SyncLock
        Catch ex As Exception

        End Try
    End Sub

    Public Sub CacheItemRemovedCallback(ByVal key As String, ByVal value As Object, ByVal reason As CacheItemRemovedReason)
        Try
            Dim dir As String = Hosting.HostingEnvironment.MapPath("~\ZedGraphImages")
            If Directory.Exists(dir) Then

                SyncLock triggerKey
                    Dim di As DirectoryInfo = New DirectoryInfo(dir)
                    Dim files As FileSystemInfo() = di.GetFileSystemInfos()
                    For Each file As FileSystemInfo In From f In files Where f.CreationTime < CType(value, DateTime)
                        Try
                            WebTools.Files.TryDelete(file.Name, 50)
                        Catch ex As Exception

                        End Try
                    Next
                End SyncLock

            End If
            Run()
        Catch ex As Exception

        End Try
    End Sub
End Module