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 데이터를 쓸 때 throw됩니다. 응답이 속성을 읽으 Length 려고 시도하고 검색할 수 없는 스트림에서 속성에 액세스하려고 하면 예외가 발생하므로 예외가 발생합니다. 자세한 내용은 CanSeek 속성을 참조하세요.