您的位置:首页 > 编程语言 > C#

c# - 使用WMI(Windows Management Instrument)取得硬件信息

2012-06-30 15:48 381 查看
这个例子只是取得一台机器的内存,更多的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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐