This documentation is archived and is not being maintained.

VirtualFile.Open Method

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 abstract Stream Open()

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 string FormatTimeStamp(DateTime time)
  return String.Format("{0} at {1}",
    time.ToLongDateString(), time.ToLongTimeString());

public override Stream Open()
  string templateFile = HostingEnvironment.ApplicationPhysicalPath + "App_Data\\template.txt";
  string pageTemplate;
  DateTime now = DateTime.Now;

  // Try to get the page template out of the cache.
  pageTemplate = (string)HostingEnvironment.Cache.Get("pageTemplate");

  if (pageTemplate == null)
    // Get the page template.
    using (StreamReader reader = new StreamReader(templateFile))
      pageTemplate = reader.ReadToEnd();

    // Set template timestamp
    pageTemplate = pageTemplate.Replace("%templateTimestamp%", 

    // Make pageTemplate dependent on the template file.
    CacheDependency cd = new CacheDependency(templateFile);

    // Put pageTemplate into cache for maximum of 20 minutes.
    HostingEnvironment.Cache.Add("pageTemplate", pageTemplate, cd,
      new TimeSpan(0, 20, 0),
      CacheItemPriority.Default, null);

  // Put the page data into the template.
  pageTemplate = pageTemplate.Replace("%file%", this.Name);
  pageTemplate = pageTemplate.Replace("%content%", content);

  // Get the data time stamp from the cache.
  DateTime dataTimeStamp = (DateTime)HostingEnvironment.Cache.Get("dataTimeStamp");
  pageTemplate = pageTemplate.Replace("%dataTimestamp%", 
  pageTemplate = pageTemplate.Replace("%pageTimestamp%", 

  // Put the page content on the stream.
  Stream stream = new MemoryStream();
  StreamWriter writer = new StreamWriter(stream);

  stream.Seek(0, SeekOrigin.Begin);

  return stream;

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.