这个例子只是取得一台机器的内存,更多的Win32 对象在下面,您可以用同样的方式取得这些信息。
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394197
using System.Management;
public static long GetTotalMemory(string p_MachineName, string p_Domain = null, string p_Username = null, string p_Password = null)
{
try
{
long memSize = 0;
ManagementScope connectionScope = new ManagementScope();
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = ImpersonationLevel.Impersonate;
if (p_Domain != null && p_Username != null && p_Password != null)
{
options.Username = p_Domain + @"\" + p_Username;
options.Password = p_Password;
}
else
{
options.Authentication = AuthenticationLevel.Default;
}
connectionScope.Options = options;
connectionScope.Path = new ManagementPath(@"\\" + p_MachineName + @"\root\cimv2");
connectionScope.Connect();
SelectQuery msQuery = new SelectQuery("SELECT * FROM Win32_MemoryDevice");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(connectionScope, msQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
long startAddr = Convert.ToInt64(item["StartingAddress"].ToString());
long endAddr = Convert.ToInt64(item["EndingAddress"].ToString());
memSize += (endAddr - startAddr);
}
return memSize;
}
catch (Exception ex)
{
throw ex;
}
}