问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Management;namespaceEx07_03{publicpartialclassForm1:Form{publicStringstrHardDiskID;publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_PhysicalMedia");foreach(ManagementObjectmoinsearcher.Get()){try{strHardDiskID=mo["SerialNumber"].ToString().Trim();break;}catch(NullReferenceExceptionEF){}}label2.Text=strHardDiskID;}}}我要得到硬盘序列号,可却是空的..listBox1.Items.Add(disk["Name"].ToString());
解决方案
解决方案二:
你上网搜,一大把.看看bbs.msproject.cn,codeproject.com都有;
解决方案三:
break;你把这个去掉!!
解决方案四:
这个能获取硬盘序列号,我在VS2005下测试通过using System.Management;privatestringgetHardDidskID(){ManagementObjectSearchersearcher=newManagementObjectSearcher("SelectSerialNumberFromWin32_PhysicalMedia");stringbiosNumber=null;foreach(ManagementObjectmgtinsearcher.Get()){biosNumber=mgt["SerialNumber"].ToString();}returnbiosNumber;}