HostFileChangeMonitor 類別

定義

監控目錄和檔案路徑,並通知快取受監控項目發生變更的資訊。Monitors directories and file paths and notifies the cache of changes to the monitored items. 這個類別無法被繼承。This class cannot be inherited.

public ref class HostFileChangeMonitor sealed : System::Runtime::Caching::FileChangeMonitor
public sealed class HostFileChangeMonitor : System.Runtime.Caching.FileChangeMonitor
type HostFileChangeMonitor = class
    inherit FileChangeMonitor
Public NotInheritable Class HostFileChangeMonitor
Inherits FileChangeMonitor
繼承
HostFileChangeMonitor

範例

下列範例會建立一個快取專案, 它HostFileChangeMonitor會使用物件來監視檔案系統上來源資料的狀態 (也就是檔案)。The following example creates a cache item that uses a HostFileChangeMonitor object to monitor the state of the source data (which is a file) on the file system. 快取專案是使用CacheItemPolicy物件所定義, 以提供快取專案的收回和到期詳細資料。The cache entry is defined using a CacheItemPolicy object to provide eviction and expiration details for the cache entry.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Caching;
using System.IO;

public partial class _Default : System.Web.UI.Page
{

    protected void Button1_Click1(object sender, EventArgs e)
    {
        ObjectCache cache = MemoryCache.Default;
        string fileContents = cache["filecontents"] as string;
       
        if (fileContents == null)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.AbsoluteExpiration =
                DateTimeOffset.Now.AddSeconds(10.0);

            List<string> filePaths = new List<string>();
            string cachedFilePath = Server.MapPath("~") +
                "\\cacheText.txt";

            filePaths.Add(cachedFilePath);

            policy.ChangeMonitors.Add(new
                HostFileChangeMonitor(filePaths));

            // Fetch the file contents.
            fileContents = File.ReadAllText(cachedFilePath) + "\n"
                + DateTime.Now.ToString();

            cache.Set("filecontents", fileContents, policy);

        }

        Label1.Text = fileContents;
    }
}
Imports System.Runtime.Caching
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
        Dim cache As ObjectCache = MemoryCache.Default
        Dim fileContents As String = TryCast(cache("filecontents"), _
            String)
        If fileContents Is Nothing Then
            Dim policy As New CacheItemPolicy()
            policy.AbsoluteExpiration = _
                DateTimeOffset.Now.AddSeconds(10.0)
            Dim filePaths As New List(Of String)()
            Dim cachedFilePath As String = Server.MapPath("~") & _
                "\cacheText.txt"
            filePaths.Add(cachedFilePath)
            policy.ChangeMonitors.Add(New  _
                HostFileChangeMonitor(filePaths))

            ' Fetch the file contents.
            fileContents = File.ReadAllText(cachedFilePath) & _
                vbCrLf & DateTime.Now.ToString()
            cache.Set("filecontents", fileContents, policy)
        End If
        Label1.Text = fileContents
    End Sub

End Class

備註

類別是FileChangeMonitor類型的具體執行。 HostFileChangeMonitorThe HostFileChangeMonitor class is a concrete implementation of the FileChangeMonitor type. 這個類別是密封的, 因此無法擴充。This class is sealed, therefore it cannot be extended. 如果您想要使用現有的快取實作為, 以及監視檔案和目錄的變更, 這個類別就很有用。This class is useful if you want to use an existing cache implementation and to monitor files and directories for changes.

針對每個指定的檔案或目錄路徑HostFileChangeMonitor , 如果發生下列其中一項變更, 類別會觸發變更通知:For each specified file or directory path, the HostFileChangeMonitor class triggers a change notification if any of the following changes occur:

  • 受監視的檔案或目錄的名稱會變更。The name of the monitored file or directory changes.

  • 在建立監視時, 指定的檔案或目錄不存在, 但在稍後建立。The specified file or directory did not exist at the time the monitor was created, but was created later. 換句話說, 已在受監視專案的範圍中建立檔案或目錄。In other words, a file or directory was created in the scope of the monitored items.

  • 受監視檔案的大小已變更。The size of a monitored file changed.

  • 受監視檔案的內容已變更, 或受監視目錄的內容已變更。The contents of a monitored file changed, or the contents of a monitored directory changed.

  • 檔案或目錄的存取控制清單 (ACL) 已經變更。The access control list (ACL) of the file or directory was changed.

  • 已刪除受監視的檔案或目錄。The monitored file or directory was deleted.

如果監視的檔案或目錄同時發生太多變更, HostFileChangeMonitor實例可能會失去特定變更的追蹤。If too many changes occur for the monitored file or directory at the same time, the HostFileChangeMonitor instance can lose track of specific changes. 在此案例中, HostFileChangeMonitor類別會觸發變更通知。In this scenario, the HostFileChangeMonitor class triggers a change notification. HostFileChangeMonitor實例監視目錄, 而且在短時間內, 目錄結構的範圍內發生許多變更時, 就比較可能發生此情況。This scenario is more likely to occur when the HostFileChangeMonitor instance monitors a directory, and many changes occur in the scope of the directory structure in a short period of time.

因為HostFileChangeMonitor類別的目的只是要指出某個專案已在一組受監視的檔案和目錄中變更, 所以不會將有關特定變更的詳細資料視為不會被視為重要。Because the purpose of the HostFileChangeMonitor class is only to signal that something has changed among the set of monitored files and directories, it is not considered important that details about a specific change are not captured. HostFileChangeMonitor類別的目的是要提供狀態已變更的通知, 以便收回快取專案 (或專案)。The purpose of the HostFileChangeMonitor class is to provide notification that state changed so that a cache entry (or entries) can be evicted. HostFileChangeMonitor因為類別不會確切指出變更的內容, 內部變更追蹤溢位是不相關的。Because the HostFileChangeMonitor class does not indicate exactly what changed, internal-change tracking overflow is irrelevant.

當您提供HostFileChangeMonitor實例的路徑時, 目錄和檔案路徑必須是目錄或檔案的完整路徑。When you supply paths to a HostFileChangeMonitor instance, the directory and file paths must be full paths to the directory or file. 路徑中不允許有相對路徑和萬用字元。Relative paths and wildcard characters in paths are not allowed.

HostFileChangeMonitor當類別用於 ASP.NET 應用程式時, 用來存取受監視專案的 Windows 識別就是 ASP.NET 應用程式的應用程式識別。When the HostFileChangeMonitor class is used in an ASP.NET application, the Windows identity that is used for access to monitored items is the application identity for the ASP.NET application. 換句話說, 應用程式識別將會是下列其中一項:In other words, the application identity will be one of the following:

  • 進程身分識別。The process identity.

  • 已設定的應用程式識別。The configured application identity.

  • UNC 認證 (如果應用程式是從 UNC 共用執行)。The UNC credential if the application is running from a UNC share.

在 non-ASP.NET 應用程式中使用FileSystemWatcher 類別時,會在內部使用類別來監視檔案。HostFileChangeMonitorWhen the HostFileChangeMonitor class is used in a non-ASP.NET application, the FileSystemWatcher class is used internally to monitor files. 如此一來, 任何存取控制清單 (ACL) 會套用到受監視的檔案或目錄, 就會套用至目前線程的 Windows 識別。As a result, whatever access control list (ACL) applies to a monitored file or directory is applied to the Windows identity of the current thread.

注意

呼叫端必須具有適當的代碼啟用安全性 (CAS) 許可權層級, 而且必須具有所有受監視目錄和路徑的 NTFS 許可權。Callers must have the appropriate level of code access security (CAS) permissions and must have NTFS permissions to all monitored directories and paths.

建構函式

HostFileChangeMonitor(IList<String>)

初始化 HostFileChangeMonitor 類別的新執行個體。Initializes a new instance of the HostFileChangeMonitor class.

屬性

FilePaths

取得已經傳遞給 HostFileChangeMonitor(IList<String>) 建構函式的目錄和檔案路徑集合。Gets the collection of directories and file paths that was passed to the HostFileChangeMonitor(IList<String>) constructor.

HasChanged

取得值,這個值表示 ChangeMonitor 類別所監控的狀態已變更。Gets a value that indicates that the state that is monitored by the ChangeMonitor class has changed.

(繼承來源 ChangeMonitor)
IsDisposed

取得值,這個值表示是否已經處置 ChangeMonitor 類別的衍生執行個體。Gets a value that indicates that the derived instance of a ChangeMonitor class is disposed.

(繼承來源 ChangeMonitor)
LastModified

取得唯讀值,這個值表示所監控的檔案或路徑上次寫入的時間。Gets a read-only value that indicates the last write time of a monitored file or path.

UniqueId

取得 HostFileChangeMonitor 執行個體的識別項,該識別項的基準是監控目錄和檔案路徑組。Gets an identifier for the HostFileChangeMonitor instance that is based on the set of monitored directories and file paths.

方法

Dispose()

ChangeMonitor 類別目前的執行個體所使用的資源全部釋出。Releases all resources that are used by the current instance of the ChangeMonitor class.

(繼承來源 ChangeMonitor)
Dispose(Boolean)

釋放所有的 Managed 資源和 Unmanaged 資源,以及對 ChangeMonitor 執行個體的所有參考。Releases all managed and unmanaged resources and any references to the ChangeMonitor instance. 這項多載必須由衍生自變更監控的類別實作。This overload must be implemented by derived change-monitor classes.

(繼承來源 ChangeMonitor)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializationComplete()

從衍生類別的建構函式進行呼叫,以指出初始化已經完成。Called from the constructor of derived classes to indicate that initialization is finished.

(繼承來源 ChangeMonitor)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
NotifyOnChanged(OnChangedCallback)

在相依性變更時,由快取實作呼叫來註冊回呼,並透過 ObjectCache 委派通知 OnChangedCallback 執行個體。Called by Cache implementers to register a callback and notify an ObjectCache instance through the OnChangedCallback delegate when a dependency has changed.

(繼承來源 ChangeMonitor)
OnChanged(Object)

在相依性變更時由衍生類別呼叫,以引發事件。Called by derived classes to raise the event when a dependency changes.

(繼承來源 ChangeMonitor)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於