This documentation is archived and is not being maintained.

HostFileChangeMonitor Class

Monitors directories and file paths and notifies the cache of changes to the monitored items. This class cannot be inherited.

Namespace:  System.Runtime.Caching
Assembly:  System.Runtime.Caching (in System.Runtime.Caching.dll)

public sealed class HostFileChangeMonitor : FileChangeMonitor

The HostFileChangeMonitor type exposes the following members.

Public methodHostFileChangeMonitorInitializes a new instance of the HostFileChangeMonitor class.

Public propertyFilePathsGets the collection of directories and file paths that was passed to the HostFileChangeMonitor constructor. (Overrides FileChangeMonitor.FilePaths.)
Public propertyHasChangedGets a value that indicates that the state that is monitored by the ChangeMonitor class has changed. (Inherited from ChangeMonitor.)
Public propertyIsDisposedGets a value that indicates that the derived instance of a ChangeMonitor class is disposed. (Inherited from ChangeMonitor.)
Public propertyLastModifiedGets a read-only value that indicates the last write time of a monitored file or path. (Overrides FileChangeMonitor.LastModified.)
Public propertyUniqueIdGets an identifier for the HostFileChangeMonitor instance that is based on the set of monitored directories and file paths. (Overrides ChangeMonitor.UniqueId.)

Public methodDispose()Releases all resources that are used by the current instance of the ChangeMonitor class. (Inherited from ChangeMonitor.)
Protected methodDispose(Boolean)Releases all managed and unmanaged resources and any references to the ChangeMonitor instance. This overload must be implemented by derived change-monitor classes. (Inherited from ChangeMonitor.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodInitializationCompleteCalled from the constructor of derived classes to indicate that initialization is finished. (Inherited from ChangeMonitor.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodNotifyOnChangedCalled by Cache implementers to register a callback and notify an ObjectCache instance through the OnChangedCallback delegate when a dependency has changed. (Inherited from ChangeMonitor.)
Protected methodOnChangedCalled by derived classes to raise the event when a dependency changes. (Inherited from ChangeMonitor.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The 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.

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.

  • The access control list (ACL) of the file or directory was changed.

  • The monitored file or directory was deleted.

If too many changes occur for the monitored file or directory at the same time, the HostFileChangeMonitor instance can lose track of specific changes. In this scenario, the HostFileChangeMonitor class triggers a change notification. 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.

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. The purpose of the HostFileChangeMonitor class is to provide notification that state changed so that a cache entry (or entries) can be evicted. Because the HostFileChangeMonitor class does not indicate exactly what changed, internal-change tracking overflow is irrelevant.

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.

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.

  • The UNC credential if the application is running from a UNC share.

When the HostFileChangeMonitor class is used in a non-ASP.NET application, the FileSystemWatcher class is used internally to monitor files. 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.


Callers must have the appropriate level of code access security (CAS) permissions and must have NTFS permissions to all monitored directories and paths.

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. 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 =

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



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

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


        Label1.Text = fileContents;

.NET Framework

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.