VirtualFile.Open Method ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

When overridden in a derived class, returns a read-only stream to the virtual resource.

Namespace:   System.Web.Hosting
Assembly:  System.Web (in System.Web.dll)

Public MustOverride Function Open As Stream

Return Value

Type: System.IO.Stream

A read-only stream to the virtual file.

The Open method returns a stream containing the data treated as a file by the VirtualPathProvider class. The stream is read-only and is seekable (the CanSeek property is true).

Notes to Inheritors:

In derived classes the Open method must return a seekable stream. If the method returns a stream that does not support seeking, a NotSupportedException is thrown when the stream is passed to the HttpResponse object to write out the data. The exception occurs because the response tries to read the Length property, and on a stream that is not seekable, attempting to access the property causes an exception. For more information, see the CanSeek property.

The following code example is an implementation of the Open method that combines virtual-file-specific information with a template file and then returns the combination. The template file is cached to reduce the overhead of reading the file system multiple times to retrieve the template file. For the full code required to run the example, see the Example section of the VirtualFile class overview.


Private Function FormatTimeStamp(ByVal time As DateTime) As String
  Return String.Format("{0} at {1}", _
    time.ToLongDateString(), time.ToLongTimeString)
End Function

Public Overrides Function Open() As System.IO.Stream
  Dim templateFile As String
  templateFile = HostingEnvironment.ApplicationPhysicalPath & "App_Data\template.txt"

  Dim pageTemplate As String
  Dim now As DateTime
  now = DateTime.Now

  ' Try to get the page template out of the cache.
  pageTemplate = CType(HostingEnvironment.Cache.Get("pageTemplate"), String)

  If pageTemplate Is Nothing Then
    ' Get the page template.
    Try
      pageTemplate = My.Computer.FileSystem.ReadAllText(templateFile)
    Catch fileException As Exception
      Throw fileException
    End Try

    ' Set template timestamp.
    pageTemplate = pageTemplate.Replace("%templateTimestamp%", _
      FormatTimeStamp(Now))

    ' Make pageTemplate dependent on the template file.
    Dim cd As CacheDependency
    cd = New CacheDependency(templateFile)

    ' Put pageTemplate into cache for maximum of 20 minutes.
    HostingEnvironment.Cache.Add("pageTemplate", pageTemplate, cd, _
      Cache.NoAbsoluteExpiration, _
      New TimeSpan(0, 20, 0), _
      CacheItemPriority.Default, Nothing)
  End If

  ' Put the page data into the template.
  pageTemplate = pageTemplate.Replace("%file%", Me.Name)
  pageTemplate = pageTemplate.Replace("%content%", content)

  ' Get the data timestamp from the cache.
  Dim dataTimeStamp As DateTime
  dataTimeStamp = CType(HostingEnvironment.Cache.Get("dataTimeStamp"), DateTime)
  pageTemplate = pageTemplate.Replace("%dataTimestamp%", _
    FormatTimeStamp(dataTimeStamp))

  ' Set a timestamp for the page.
  Dim pageTimeStamp As String
  pageTimeStamp = FormatTimeStamp(now)
  pageTemplate = pageTemplate.Replace("%pageTimestamp%", pageTimeStamp)

  ' Put the page content on the stream.
  Dim stream As MemoryStream
  stream = New MemoryStream()

  Dim writer As StreamWriter
  writer = New StreamWriter(stream)

  writer.Write(pageTemplate)
  writer.Flush()
  stream.Seek(0, SeekOrigin.Begin)

  Return stream
End Function

.NET Framework
Available since 2.0
Return to top
Show: