Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetSystemFileCacheSize (kernel32)
 
.
Summary
Limits the size of the working set for the file system cache.

C# Signature:

  /// <summary>Limits the size of the working set for the file system cache.</summary>
  /// <param name="MinimumFileCacheSize">The minimum size of the file cache, in bytes. To flush, use UInt64.MaxValue.</param>
  /// <param name="MaximumFileCacheSize">The maximum size of the file cache, in bytes. Must be > min + 64KB</param>
  /// <param name="Flags">See File_Cache_Flags</param>
  /// <returns>0=fail !0=success</returns>
[DllImport("kernel32.dll", SetLastError=true)]
static extern Int32 SetSystemFileCacheSize(IntPtr MinimumFileCacheSize, IntPtr MaximumFileCacheSize, File_Cache_Flags Flags);

VB Signature:

Declare Function SetSystemFileCacheSize Lib "kernel32.dll" (TODO) As TODO

User-Defined Types:

/// <summary>Flags for use with SetSystemFileCacheSize.  Note that corresponding enable & disable are mutually exclusive and will fail.</summary>
[Flags]
public enum File_Cache_Flags : uint
{
    MAX_HARD_ENABLE =    0x00000001,
    MAX_HARD_DISABLE =    0x00000002,
    MIN_HARD_ENABLE =    0x00000004,
    MIN_HARD_DISABLE =    0x00000008,
}

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

  /// <summary>Sets or unsets File Cache Size</summary>
  /// <param name="MinimumFileCacheSize">Minimum Size, or zero for disable minimum</param>
  /// <param name="MaximumFileCacheSize">Maximum Size, or zero for disable maximum</param>
  public static void SetSystemFileCacheSize(ulong MinimumFileCacheSize, ulong MaximumFileCacheSize)
  {
      if (Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor < 2))
          throw new NotImplementedException("Windows 5.2 (XP 64/2003 and up) is required for File System Cache Control");
      if (IntPtr.Size == 4 && MinimumFileCacheSize > UInt32.MaxValue)
          throw new InvalidOperationException("Minimum Size is Invalid - SetSystemFileCacheSize");
      if (IntPtr.Size == 4 && MaximumFileCacheSize > UInt32.MaxValue)
          MaximumFileCacheSize = UInt32.MaxValue;
      File_Cache_Flags
          flags = (File_Cache_Flags)0;

      if (MinimumFileCacheSize > 0)
          flags |= File_Cache_Flags.MIN_HARD_ENABLE;
          flags &= File_Cache_Flags.MIN_HARD_ENABLE;
      else
          flags |= File_Cache_Flags.MIN_HARD_DISABLE;
          flags &= File_Cache_Flags.MIN_HARD_DISABLE;

      if (MaximumFileCacheSize > 0 && MaximumFileCacheSize>MinimumFileCacheSize + 64 * 1024) // min must be <64K lower
          flags |= File_Cache_Flags.MAX_HARD_ENABLE;
          flags &= File_Cache_Flags.MAX_HARD_ENABLE;
      else
          flags |= File_Cache_Flags.MAX_HARD_DISABLE;
          flags &= File_Cache_Flags.MAX_HARD_DISABLE;
      int
          result = 0;

      if(IntPtr.Size!=4)
          result = SetSystemFileCacheSize(new IntPtr((long)MinimumFileCacheSize), new IntPtr((long)MaximumFileCacheSize), flags);
      else
          result = SetSystemFileCacheSize(new IntPtr((int)MinimumFileCacheSize), new IntPtr((int)MaximumFileCacheSize), flags);

      if (result == 0)
          throw new Win32Exception();
  }

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions