You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C# .NET6.0修改Windows虚拟内存(页面文件)大小?

页面文件大小设置问题(C# .NET6.0)

场景与需求

Windows默认设置为「Automatically manage paging file size for all drives」。当前设备是公共区域无固定用户的办公/业务学习用机,故障时由值班人员用Ghost镜像重装并一键优化系统;设备包含4G内存64G硬盘的老旧瘦终端,即将换新设备及管理方案,目前需临时过渡优化程序。

需求:通过C# .NET6.0将页面文件大小设为物理内存的20%-50%(测试机物理内存为32GB)。

尝试的方法及问题

1. WMIC命令调用方式

查询得知以下WMIC命令手动执行可实现需求:

wmic COMPUTERSYSTEM set AutomaticManagedPagefile=false
wmic PAGEFILESET where name ="C:\\pagefile.sys" set InitialSize=1638,MaximumSize=4095

在Windows10 2019 LTSC手动执行以上命令有效,但通过以下C#代码执行时仅第一条命令生效,重启后页面文件设置不符合预期:

internal static void ExecuteCmd(string command)
{
    try
    {
        Process process = new();
        ProcessStartInfo startInfo = new()
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            CreateNoWindow = true,
            FileName = "cmd.exe",
            Arguments = "/c " + command,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
        };
        process.StartInfo = startInfo;
        process.Start();
    }
    catch(Exception e)
    {
        LogHelper.Error(e.Message);
    }
}

internal static void ChangeSystemPageFileSize()
{
    string? TotalPhysicalMemory = "0";
    string? InitialSize = "0";
    string? MaximumSize = "0";

    try
    {
        ManagementObjectSearcher Search = new ManagementObjectSearcher();
        Search.Query = new ObjectQuery("Select * From Win32_ComputerSystem");
        foreach (ManagementObject obj in Search.Get().Cast<ManagementObject>())
        {
            TotalPhysicalMemory = $"{Math.Round(Convert.ToDouble(obj["TotalPhysicalMemory"]) / (1024 * 1024))}";

            if (!string.IsNullOrWhiteSpace(TotalPhysicalMemory))
            {
                break;
            }
        }
        InitialSize = Math.Floor(int.Parse(TotalPhysicalMemory) * 0.2).ToString().Trim();
        MaximumSize = Math.Floor(int.Parse(TotalPhysicalMemory) * 0.5).ToString().Trim();
        CommandHelper.ExecuteCmd("wmic COMPUTERSYSTEM set AutomaticManagedPagefile=false");
        CommandHelper.ExecuteCmd("wmic PAGEFILESET where name =\"C:\\\\pagefile.sys\" set InitialSize=" + InitialSize +",MaximumSize=" + MaximumSize);
    }
    catch (Exception e)
    {
        LogHelper.Error(e.Message);
    }
}

2. WMI直接操作方式

尝试通过WMI直接更新页面文件设置,仍无法修改InitialSize、MaximumSize及Name属性,代码如下:

InitialSize = (UInt32)Math.Floor(TotalPhysicalMemory * 0.2);
MaximumSize = (UInt32)Math.Floor(TotalPhysicalMemory * 0.5);
ManagementObject ComputerSystem = new($@"ROOT\CIMV2:Win32_ComputerSystem.Name='{Environment.MachineName}'");
ComputerSystem["AutomaticManagedPagefile"] = false;
ComputerSystem.Put();
ManagementObject PageFileSetting = new($@"ROOT\CIMV2:Win32_PageFileSetting");
PageFileSetting.SetPropertyValue("Name", "C:\\PAGEFILE.SYS");
PageFileSetting.SetPropertyValue("InitialSize", InitialSize);
PageFileSetting.SetPropertyValue("MaximumSize", MaximumSize);
PageFileSetting["Name"] = "C:\\PAGEFILE.SYS";
PageFileSetting["InitialSize"] = InitialSize;
PageFileSetting["MaximumSize"] = MaximumSize;
PageFileSetting.Put();

恳请提供解决建议。


内容的提问来源于stack exchange,提问作者Jerry Dora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 04:10:27