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 性質為真)。
給實施者的注意事項
在導出類別中,方法 Open() 必須回傳可尋道的串流。 若方法回傳的串流不支援尋道,當串流傳遞給HttpResponse物件以寫入資料時,會拋出 aNotSupportedException。 例外發生是因為回應嘗試讀取該 Length 屬性,而在無法尋求的串流中,嘗試存取該屬性會導致異常。 如需詳細資訊,請參閱 CanSeek 屬性 (Property)。