VirtualFile.Open 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,返回到虚拟资源的只读流。
public:
abstract System::IO::Stream ^ Open();
public abstract System.IO.Stream Open ();
abstract member Open : unit -> System.IO.Stream
Public MustOverride Function Open () As Stream
返回
到虚拟文件的只读流。
示例
下面的代码示例是 方法的 Open 实现,该方法将特定于虚拟文件的信息与模板文件合并,然后返回组合。 缓存模板文件以减少多次读取文件系统以检索模板文件的开销。 有关运行该示例所需的完整代码,请参阅类概述的 VirtualFile “示例”部分。
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%",
FormatTimeStamp(now));
// 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,
Cache.NoAbsoluteExpiration,
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%",
FormatTimeStamp(dataTimeStamp));
pageTemplate = pageTemplate.Replace("%pageTimestamp%",
FormatTimeStamp(now));
// Put the page content on the stream.
Stream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(pageTemplate);
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
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
注解
方法 Open 返回一个流,其中包含类 VirtualPathProvider 视为文件的数据。 流是只读的,并且可查找 (属性 CanSeek 为 true) 。
实施者说明
在派生类中, Open() 方法必须返回可查找流。 如果方法返回不支持查找的流, NotSupportedException 则在将流传递给 HttpResponse 对象以写出数据时,将引发 。 发生异常是因为响应尝试读取 Length 属性,在不可查找的流上,尝试访问属性会导致异常。 有关更多信息,请参见 CanSeek 属性。