C# WMI取硬件信息,求大神解释!
取出主板信息,其中Form1里面的testbox1文本存放结果,以下是类信息(声明,此类取内存,硬盘,等信息正常,因此复制取主板值的代码)privateManagemen...
取出主板信息,其中Form1里面的testbox1文本存放结果,以下是类信息(声明,此类取内存,硬盘,等信息正常,因此复制取主板值的代码)
private ManagementClass mc;
private ManagementObjectCollection moc;
public GetSystemInfo()
{ }
public string GetBaseBoardInfo()
{
mc = new ManagementClass("Win32_BaseBoard");
moc = mc.GetInstances();
string BaseBoardInfo = string.Empty;
foreach(ManagementObject mo in moc)
{
BaseBoardInfo = "主板模式:" + mo.Properties["Name"].Value.ToString()+"\n"+
"主板型号:" + mo.Properties["Manufacturer"].Value.ToString()+"\n"+
"主板名称:" + mo.Properties["Name"].Value.ToString()+"\n"+
"主板型号:" + mo.Properties["SerialNumber"].Value.ToString();
}
moc = null;
mc = null;
return BaseBoardInfo;
}
再提问一个问题,如果有双硬盘,应该怎么取。现在只能取到一个。 展开
private ManagementClass mc;
private ManagementObjectCollection moc;
public GetSystemInfo()
{ }
public string GetBaseBoardInfo()
{
mc = new ManagementClass("Win32_BaseBoard");
moc = mc.GetInstances();
string BaseBoardInfo = string.Empty;
foreach(ManagementObject mo in moc)
{
BaseBoardInfo = "主板模式:" + mo.Properties["Name"].Value.ToString()+"\n"+
"主板型号:" + mo.Properties["Manufacturer"].Value.ToString()+"\n"+
"主板名称:" + mo.Properties["Name"].Value.ToString()+"\n"+
"主板型号:" + mo.Properties["SerialNumber"].Value.ToString();
}
moc = null;
mc = null;
return BaseBoardInfo;
}
再提问一个问题,如果有双硬盘,应该怎么取。现在只能取到一个。 展开
展开全部
你那个foreach中结果保存的有问题,你实际上只取到了最后的信息,应该类似于这样
public List<string> GetBaseBoardInfo()
{
mc = new ManagementClass("Win32_BaseBoard");
moc = mc.GetInstances();
List<string> BaseBoardInfo =new List<string>();
foreach(ManagementObject mo in moc)
{
BaseBoardInfo.Add("主板模式:" + mo.Properties["Name"].Value.ToString()+"\n"+
"主板型号:" + mo.Properties["Manufacturer"].Value.ToString()+"\n"+
"主板名称:" + mo.Properties["Name"].Value.ToString()+"\n"+
"主板型号:" + mo.Properties["SerialNumber"].Value.ToString());
}
moc = null;
mc = null;
return BaseBoardInfo;
}
然后在Form1中比如放一个richtextbox控件
List<string> list=GetBaseBoardInfo();
foreach(string str in list)
richTextBox1.AppendText(str+"\r\n\r\n");
更多追问追答
追问
我执行你这一段有问题,你试试,还是取不出来哦。
追答
有问题是什么意思,总该有具体错误吧。
展开全部
/// <summary>
/// 取第一块硬盘编号
/// </summary>
/// <returns></returns>
internal string GetHardDiskID()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
string strHardDiskID = null;
foreach (ManagementObject mo in searcher.Get())
{
if (mo["Tag"].ToString().Trim() != "\\\\.\\PHYSICALDRIVE0") continue;
string t = mo["SerialNumber"].ToString().Trim();
try
{
for (int i = 0; i < t.Length / 4; i++)
{
string h = t.Substring(i * 4 + 2, 2);
string l = t.Substring(i * 4, 2);
byte[] array = new byte[2];
array[0] = byte.Parse(h, System.Globalization.NumberStyles.AllowHexSpecifier);
array[1] = byte.Parse(l, System.Globalization.NumberStyles.AllowHexSpecifier);
strHardDiskID += Convert.ToString(System.Text.Encoding.ASCII.GetString(array));
}
}
catch
{
strHardDiskID = t.Trim();
}
break;
}
return strHardDiskID.Trim();
}
catch
{
return string.Empty;
}
}
/// 取第一块硬盘编号
/// </summary>
/// <returns></returns>
internal string GetHardDiskID()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
string strHardDiskID = null;
foreach (ManagementObject mo in searcher.Get())
{
if (mo["Tag"].ToString().Trim() != "\\\\.\\PHYSICALDRIVE0") continue;
string t = mo["SerialNumber"].ToString().Trim();
try
{
for (int i = 0; i < t.Length / 4; i++)
{
string h = t.Substring(i * 4 + 2, 2);
string l = t.Substring(i * 4, 2);
byte[] array = new byte[2];
array[0] = byte.Parse(h, System.Globalization.NumberStyles.AllowHexSpecifier);
array[1] = byte.Parse(l, System.Globalization.NumberStyles.AllowHexSpecifier);
strHardDiskID += Convert.ToString(System.Text.Encoding.ASCII.GetString(array));
}
}
catch
{
strHardDiskID = t.Trim();
}
break;
}
return strHardDiskID.Trim();
}
catch
{
return string.Empty;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询