HostFileChangeMonitor Class

.NET Framework (current version)

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


Initializes a new instance of the HostFileChangeMonitor class.


Gets the collection of directories and file paths that was passed to the HostFileChangeMonitor constructor.(Overrides FileChangeMonitor.FilePaths.)


Gets a value that indicates that the state that is monitored by the ChangeMonitor class has changed.(Inherited from ChangeMonitor.)


Gets a value that indicates that the derived instance of a ChangeMonitor class is disposed.(Inherited from ChangeMonitor.)


Gets a read-only value that indicates the last write time of a monitored file or path.(Overrides FileChangeMonitor.LastModified.)


Gets an identifier for the HostFileChangeMonitor instance that is based on the set of monitored directories and file paths. (Overrides ChangeMonitor.UniqueId.)


Releases all resources that are used by the current instance of the ChangeMonitor class.(Inherited from ChangeMonitor.)


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Called by Cache implementers to register a callback and notify an ObjectCache instance through the OnChangedCallback delegate when a dependency has changed. (Inherited from ChangeMonitor.)


Returns 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
Available since 4.0

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

Return to top