In Windows default settings, the pagefile size is set to Automatically manage paging file size for all drives
.
This project is quite special. It is not maintained by operators. These computers are only provided for internal personnel to handle business or learn business processes. These computers have no fixed users, they are placed in public areas. When they have problems, the person on duty will reinstall the system using the fixed ghost file and execute the script to optimize the system with one click. These devices are old, and there are some thin terminals with 4G memory and 64G hard disk. Before long, new devices will replace them and have new management solutions. As a temporary transitional scheme before replacing the new scheme, I think providing a simple "optimization" program will be the simplest way to deal with it at present.
I want to change the page file size to 20% - 50% of the physical memory through C# .net-6.0 like (Test Computer's Physical memory is 32 GB)
I checked some information and it seems that wmic
can meet my needs.
wmic COMPUTERSYSTEM set AutomaticManagedPagefile=false
wmic PAGEFILESET where name ="C:\\pagefile.sys" set InitialSize=1638,MaximumSize=4095
I tested these two lines of commands on Windows10 2019 LTSC, and they worked well.But when I use code to execute,I have some problems.Here is my code:
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);
}
I have obtained the correct physical memory size, but only the first command executed takes effect.When computer restart,I get windows settings like .
Thanks @ProgrammingLlama @user9938 I updated the page file size setting through WMI. There may be a little problem:
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();
Neither the SetPropertyValue
method nor the Put
method can update the value of InitialSize
MaximumSize
Name
Maybe someone can give me some advice?