我们使用 zedgraph webcontrols 在我们的 asp.net (vb) web 应用程序中绘制图表。生成图像的临时目录已满,现在已达到 2GB+。
有没有办法自动清除这个目录,还是我必须实施一个讨厌的HttpContext.Cache expired callback
黑客来从那里删除旧文件?
谢谢,弗洛
因此,我没有亲自动手编辑和重新编译 zedgraphweb http://sourceforge.net/projects/zedgraph,而是选择了一种更常见的方法,自己处理大量文件。我正在使用 Cache 的 CacheItemRemovedCallback 在 Web 应用程序中执行代码,例如使用计时器。此外,此代码会重新填充缓存项,因此会更新循环。瞧:在 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