Export (0) Print
Expand All

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.

  NameDescription
Public methodHostFileChangeMonitorInitializes a new instance of the HostFileChangeMonitor class.
Top

  NameDescription
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.)
Top

  NameDescription
Public methodDispose()Releases all resources that are used by the current instance of the ChangeMonitor class. (Inherited from ChangeMonitor.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (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.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

NoteNote

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

.NET Framework

Supported in: 4.5, 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft