英語で読む

次の方法で共有


VirtualFile クラス

定義

仮想ファイルまたはリソース領域のファイル オブジェクトを表します。

C#
public abstract class VirtualFile : System.Web.Hosting.VirtualFileBase
継承

次のコード例は、オブジェクトに VirtualFile 格納されている情報とテンプレート ファイルを DataSet 組み合わせて HTML データを返すクラス実装です。 このコード例では、 クラスと VirtualDirectory クラスのコード例VirtualPathProviderを使用して、オブジェクトに読み込まれるデータ ストアから仮想リソースをDataSet提供します。 この例をコンパイルして実行するための完全な手順については、クラスの概要の「例」セクションを VirtualPathProvider 参照してください。

この例には、クラス実装、オブジェクトの VirtualFile 設定に使用される XML データ ファイル、ページ テンプレート ファイルの DataSet 3 つの部分があります。

最初のコード例は、 クラスの VirtualFile 実装です。 そのコンストラクターは、カスタム VirtualPathProvider オブジェクトの メソッドを使用して オブジェクトを DataSet 返します。 次に、 オブジェクトを DataSet 検索して、指定された仮想ファイル パスに関連付けられている情報を取得します。 メソッドでは Open 、 オブジェクトの DataSet 情報をテンプレート ファイルと組み合わせて、その組み合わせを オブジェクトとして Stream 返します。

C#
using System;
using System.Data;
using System.IO;
using System.Security.Permissions;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;

namespace Samples.AspNet.CS
{
  [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  public class SampleVirtualFile : VirtualFile
  {
    private string content;
    private SamplePathProvider spp;

    public bool Exists
    {
      get { return (content != null); }
    }

    public SampleVirtualFile(string virtualPath, SamplePathProvider provider)
      : base(virtualPath)
    {
      this.spp = provider;
      GetData();
    }

    protected void GetData()
    {
      // Get the data from the SamplePathProvider
      DataSet ds = spp.GetVirtualData();

      // Get the virtual file from the resource table.
      DataTable files = ds.Tables["resource"];
      DataRow[] rows = files.Select(
        String.Format("(name = '{0}') AND (type='file')", this.Name));

      // If the select returned a row, store the file contents.
      if (rows.Length > 0)
      {
        DataRow row = rows[0];

        content = row["content"].ToString();
      }
    }

    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;
    }
  }
}

2 番目の例は、カスタム VirtualPathProvider オブジェクトによって返されるオブジェクトをDataSet設定するために使用される XML データ ファイルです。 この XML データは、、、および VirtualDirectory クラスをVirtualFileVirtualPathProvider使用して外部データからデータを取得する方法を示すために使用され、運用品質のデータ ストアを表すものではありません。

<?xml version="1.0" encoding="utf-8" ?>  
<resource type="dir"   
          path="/vrDir"   
          parentPath=""   
          content="">  
  <resource type="file"   
            path="/vrDir/Level1FileA.vrf"  
            parentPath="/vrDir"   
            content="This is the content of file Level1FileA.">  
  </resource>  
  <resource type="file"   
            path="/vrDir/Level1FileB.vrf"  
            parentPath="/vrDir"   
            content="This is the content of file Level1FileB.">  
  </resource>  
  <resource type="dir"   
            path="/vrDir/Level2DirA"   
            parentPath="/vrDir"   
            content="">  
    <resource type="file"   
              path="/vrDir/Level2DirA/Level2FileA.vrf"   
              parentPath="/vrDir/Level2DirA"   
              content="This is the content of file Level2FileA.">  
    </resource>  
    <resource type="file"   
              path="/vrDir/Level2DirA/Level2FileB.vrf"  
              parentPath="/vrDir/Level2DirA"   
              content="This is the content of file Level2FileB.">  
    </resource>  
  </resource>  
  <resource type="dir"   
            path="/vrDir/Level2DirB"   
            parentPath="/vrDir"   
            content="">  
    <resource type="file"   
              path="/vrDir/Level2DirB/Level2FileA.vrf"   
              parentPath="/vrDir/Level2DirB"   
              content="This is the content of file Level2FileA.">  
    </resource>  
    <resource type="file"   
              path="/vrDir/Level2DirB/Level2FileB.vrf"  
              parentPath="/vrDir/Level2DirB"   
              content="This is the content of file Level2FileB.">  
    </resource>  
  </resource>  
</resource>  

3 番目の例は、仮想ファイルのテンプレートとして使用されるテキスト ファイルです。 ファイル内のプレースホルダーは、 や %content%などの%file%パーセント (%) マークの間のテキストで表されます。 タイムスタンプは、キャッシュされた仮想ファイル データへの変更を監視するために使用されます。

<html>  
  <head>  
    <title>File name: %file%</title>  
  </head>  

  <body>  
    <h1>%file%</h1>  
    <p>%content%</p>  
    <p>Page timestamp: %pageTimestamp%<br>  
       Data timestamp: %dataTimestamp%<br>  
       Template timestamp: %templateTimestamp%</p>  
  </body>  
</html>  

注釈

クラスは VirtualFile 、仮想ファイル システム内のファイルを表すオブジェクトの基本クラスです。 通常は、Web アプリケーション内のオブジェクトの VirtualFile 子孫ごとに VirtualPathProvider 、 クラスの子孫を実装します。

注意 (実装者)

クラスから VirtualFile 継承する場合は、 メソッドを Open() オーバーライドして、仮想リソースのコンテンツに読み取り専用ストリームを返す必要があります。

コンストラクター

VirtualFile(String)

VirtualFile クラスの新しいインスタンスを初期化します。

プロパティ

IsDirectory

ファイルとして扱う必要がある仮想リソースであることを示す値を取得します。

Name

仮想リソースの表示名を取得します。

(継承元 VirtualFileBase)
VirtualPath

仮想ファイルのパスを取得します。

(継承元 VirtualFileBase)

メソッド

CreateObjRef(Type)

リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。

(継承元 MarshalByRefObject)
Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetLifetimeService()
古い.

対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InitializeLifetimeService()

リースが作成されないようにすることで、VirtualFileBase インスタンスに無期限の有効期間を指定します。

(継承元 VirtualFileBase)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
MemberwiseClone(Boolean)

現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。

(継承元 MarshalByRefObject)
Open()

派生クラスでオーバーライドされた場合、仮想リソースへの読み取り専用のストリームを返します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1