Get system memory with C #

xiaoxiao2021-03-06  18

Open Visual Studio.Net 2003, click File - New - Project - Console Application, Language Select C #, add the following method in the program, then join in the main () function

System.console.writeline ("System Memory is:" getPhisicalMemory (). TOSTRING ());

-------------------------------------------------- --------

Private int getPhisicalmemory ()

{

ManagementObjectSearcher Searcher = new managementObjectSearcher (); // Used to query some management objects such as system information

Searcher.Query = New SelectQuery ("Win32_PhysicalMemory", "", New String [] {"Capacity"}); // Setting the query condition

ManagementObjectCollection Collection = Searcher.get (); // Get memory capacity

ManagementObjectCollection.ManagementObjectCollection.ManagementObjectEnumerator em = Collection.GeteNumerator ();

INT CAPACITY = 0;

WHILE (em.movenext ())

{

ManagementBaseObject BaseObj = em.current;

IF (Baseobj.Properties ["Capacity"]. Value! = null)

{

Try

{

Capacity = int.parse (BaseObj.properties "[" Capacity "]. Value.toString ());

}

Catch

{

Messagebox.show ("There is an error! Please contact the software author! / N", "error message", MessageBoxButtons.ok, MessageBoxicon.Error, MessageBoxDefaultButton.Button1);

Return 0;

}

}

}

Return Capacity;

转载请注明原文地址:https://www.9cbs.com/read-40534.html

New Post(0)