首页  编辑  

获得本机内存使用情况

Tags: /C#/API调用/   Date Created:

//

using Microsoft . VisualBasic . Devices;

Computer vComputer = new Computer ();

StringBuilder vMessage = new StringBuilder ();

vMessage . Append(string . Format( "{0}\r\n" ,

   vComputer . Info . TotalVirtualMemory));

vMessage . Append(string . Format( "{0}\r\n" ,

   vComputer . Info . AvailableVirtualMemory));

vMessage . Append(string . Format( "{0}\r\n" ,

   vComputer . Info . TotalPhysicalMemory));

vMessage . Append(string . Format( "{0}\r\n" ,

   vComputer . Info . AvailablePhysicalMemory));

MessageBox . Show(vMessage . ToString());

// or

using System . Runtime . InteropServices;

public struct MEMORYSTATUS

{

    public uint dwLength;

    public uint dwMemoryLoad;

    public uint dwTotalPhys;

    public uint dwAvailPhys;

    public uint dwTotalPageFile;

    public uint dwAvailPageFile;

    public uint dwTotalVirtual;

    public uint dwAvailVirtual;

};

[ DllImport ( "kernel32.dll" )]

public static extern void GlobalMemoryStatus( ref MEMORYSTATUS lpBuffer);

public void button1_Click( object sender , EventArgs e)

{

    MEMORYSTATUS vBuffer = new MEMORYSTATUS ();

   GlobalMemoryStatus(ref vBuffer);

    StringBuilder vMessage = new StringBuilder ();

   vMessage . Append(string . Format( "{0}\r\n" , vBuffer . dwTotalVirtual));

   vMessage . Append(string . Format( "{0}\r\n" , vBuffer . dwAvailVirtual));

   vMessage . Append(string . Format( "{0}\r\n" , vBuffer . dwTotalPhys));

   vMessage . Append(string . Format( "{0}\r\n" , vBuffer . dwAvailPhys));

    MessageBox . Show(vMessage . ToString());

}