N
Nathan Sokalski
I have a directory on my site that I keep a bunch of text files in (this
directory is "/poetry/poems/"). The Application keeps the first line of each
of these files in an HttpApplicationState variable as a SortedList. When I
add or modify a file in this directory, I want to delete this
HttpApplicationState variable. I tried to do this using the following lines
of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWatcher(HttpContext.Current.Server.MapPath("/poetry/poems/"))
Private Sub PoemDirModified(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles poemfilewatcher.Changed,
poemfilewatcher.Created, poemfilewatcher.Deleted
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Remove("poemlist")
HttpContext.Current.Application.UnLock()
End Sub
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher.IncludeSubdirectories = True
poemfilewatcher.EnableRaisingEvents = True
End Sub
The Function that I use to return this SortedList, whether it is stored in
an HttpApplicationState variable or not, is the following, which is also
located in Global.asax.vb:
Public Shared Function GetPoems() As SortedList 'Key=Title, Value=File
If HttpContext.Current.Application("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitiveComparer)
Dim poemfiles As String() =
System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("/poetry/poems/"))
Dim poemstreamreader As System.IO.StreamReader
Poems.Capacity = 50 'Not quite 50 poems, so call Poems.TrimToSize()
before caching
For Each poemfile As String In poemfiles
poemstreamreader = System.IO.File.OpenText(poemfile)
Poems.Add(poemstreamreader.ReadLine(), poemfile)
poemstreamreader.Close()
Next
Poems.TrimToSize()
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Add("poemlist", Poems)
HttpContext.Current.Application.UnLock()
Return Poems
Else
Return CType(HttpContext.Current.Application("poemlist"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does not
seem to delete the HttpApplicationState variable. Am I forgetting to do
something? Am I doing something wrong? Any help would be appreciated, or
possibly an example that mentions all the necessary & required steps. Thank
you to anyone who can give me any help with this.
directory is "/poetry/poems/"). The Application keeps the first line of each
of these files in an HttpApplicationState variable as a SortedList. When I
add or modify a file in this directory, I want to delete this
HttpApplicationState variable. I tried to do this using the following lines
of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWatcher(HttpContext.Current.Server.MapPath("/poetry/poems/"))
Private Sub PoemDirModified(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles poemfilewatcher.Changed,
poemfilewatcher.Created, poemfilewatcher.Deleted
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Remove("poemlist")
HttpContext.Current.Application.UnLock()
End Sub
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher.IncludeSubdirectories = True
poemfilewatcher.EnableRaisingEvents = True
End Sub
The Function that I use to return this SortedList, whether it is stored in
an HttpApplicationState variable or not, is the following, which is also
located in Global.asax.vb:
Public Shared Function GetPoems() As SortedList 'Key=Title, Value=File
If HttpContext.Current.Application("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitiveComparer)
Dim poemfiles As String() =
System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("/poetry/poems/"))
Dim poemstreamreader As System.IO.StreamReader
Poems.Capacity = 50 'Not quite 50 poems, so call Poems.TrimToSize()
before caching
For Each poemfile As String In poemfiles
poemstreamreader = System.IO.File.OpenText(poemfile)
Poems.Add(poemstreamreader.ReadLine(), poemfile)
poemstreamreader.Close()
Next
Poems.TrimToSize()
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Add("poemlist", Poems)
HttpContext.Current.Application.UnLock()
Return Poems
Else
Return CType(HttpContext.Current.Application("poemlist"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does not
seem to delete the HttpApplicationState variable. Am I forgetting to do
something? Am I doing something wrong? Any help would be appreciated, or
possibly an example that mentions all the necessary & required steps. Thank
you to anyone who can give me any help with this.