c#获取系统文件缓存大小

时间:2011-05-05 13:52:57

标签: c# pinvoke

我正在尝试获取当前系统文件缓存大小,如下所示。但是,当我运行此代码时,没有任何内容返回,有人可以看到我出错的地方吗? 仅供参考,链接为GetSystemFileCache

    [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern bool GetSystemFileCacheSize(
        ref IntPtr lpMinimumFileCacheSize,
        ref IntPtr lpMaximumFileCacheSize,
        ref IntPtr lpFlags
        );

    static void Main(string[] args)
    {            
        IntPtr lpMinimumFileCacheSize = IntPtr.Zero;
        IntPtr lpMaximumFileCacheSize = IntPtr.Zero;
        IntPtr lpFlags = IntPtr.Zero;

        bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags);
    }

3 个答案:

答案 0 :(得分:5)

        bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags);

        Console.WriteLine(lpMinimumFileCacheSize);
        Console.WriteLine(lpMaximumFileCacheSize);

对我来说很好。

输出:

1048576
2143289344

Windows 7 Pro x32

答案 1 :(得分:2)

此代码段有效:(Windows 7 x86) enter image description here

答案 2 :(得分:2)

由于我们遇到与MS KB 976618相关的问题,并且由于不想在生产服务器上运行来自未知第三方的二进制代码,我被迫使用c#语言。因为我是c#的新手,它花了我一段时间,但我终于想出了人们发布的代码片段周围的额外部分。所以编译和运行的完整的ms visual studio c#控制台程序是:

using System;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern bool GetSystemFileCacheSize(
        ref IntPtr lpMinimumFileCacheSize,
        ref IntPtr lpMaximumFileCacheSize,
        ref IntPtr lpFlags
        );

    static void Main(string[] args)
    {
        IntPtr lpMinimumFileCacheSize = IntPtr.Zero;
        IntPtr lpMaximumFileCacheSize = IntPtr.Zero;
        IntPtr lpFlags = IntPtr.Zero;

        bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags);

        Console.WriteLine(b);
        Console.WriteLine(lpMinimumFileCacheSize);
        Console.WriteLine(lpMaximumFileCacheSize);
        Console.WriteLine(lpFlags);
    }
}

并在我的Windows 7 x64 PC上输出

True
1048576
1099511627776
0

并且不,那不是我们的生产服务器。

然后在powershell中这是

$source = @"
using System;
using System.Runtime.InteropServices;

namespace MyTools
{
    public static class cache
    {

        [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool GetSystemFileCacheSize(
            ref IntPtr lpMinimumFileCacheSize,
            ref IntPtr lpMaximumFileCacheSize,
            ref IntPtr lpFlags
            );

        public static bool Get( ref IntPtr a, ref IntPtr c, ref IntPtr d )
        {
            IntPtr lpMinimumFileCacheSize = IntPtr.Zero;
            IntPtr lpMaximumFileCacheSize = IntPtr.Zero;
            IntPtr lpFlags = IntPtr.Zero;

            bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags);

            a = lpMinimumFileCacheSize;
            c = lpMaximumFileCacheSize;
            d = lpFlags;
            return b;
        }
    }
}
"@

Add-Type -TypeDefinition $source -Language CSharp

# Init variables
$SFCMin = 0
$SFCMax = 0
$SFCFlags = 0
$b = [MyTools.cache]::Get( [ref]$SFCMin, [ref]$SFCMax, [ref]$SFCFlags )

#typecast values so we can do some math with them
$SFCMin = [long]$SFCMin
$SFCMax = [long]$SFCMax
$SFCFlags = [long]$SFCFlags

write-output "Return values from GetSystemFileCacheSize are: "
write-output "Function Result : $b"
write-output "            Min : $SFCMin"
write-output ("            Max : $SFCMax ( " + $SFCMax / 1024 / 1024 / 1024 + " GiB )")
write-output "          Flags : $SFCFlags"

下一步:SetSystemFileCacheSize,我为此编写了一个powershell脚本并将其放在https://serverfault.com/questions/325277/windows-server-2008-r2-metafile-ram-usage/527466#527466