0

We use zedgraph webcontrols for charting in our asp.net (vb) web app. the temp directory for generated images fills up and now has reached 2GB+.

Is there a way to automatically clear this directory or do I have to implement a nasty HttpContext.Cache expired callback hack to delete old files from there?

Thanks, Flo

Elementenfresser
  • 711
  • 5
  • 18

1 Answers1

1

So rather than getting my hands dirty editing and recompiling zedgraphweb http://sourceforge.net/projects/zedgraph, I chose a more common apporach, dealing with the amount of files myself. I am using the Cache's CacheItemRemovedCallback to execute code in a web app like using a timer. Aslo this code refills the cache item and therefore renews the cycle. voila: disk cleanup every 5 minutes in a web app.

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
Elementenfresser
  • 711
  • 5
  • 18