日期:2014-05-19  浏览次数:20746 次

取硬盘信息出错为何
public   String   GetHardDiskID()  
    {
      try
      {
        ManagementObjectSearcher   searcher   =   new   ManagementObjectSearcher( "SELECT   *   FROM   Win32_PhysicalMedia ");
        String   strHardDiskID   =   null   ;
        foreach(ManagementObject   mo   in   searcher.Get())  
        {        
          strHardDiskID   =   mo[ "SerialNumber "].ToString().Trim();
          break;                    
        }
        return   strHardDiskID   ;
      }
      catch
      {
        return   " ";
      }
    }//end

------解决方案--------------------
private void buttonServiceTag_Click(object sender, System.EventArgs e)
{
textBoxOutput.Text = " ";
ManagementScope scope = new ManagementScope( "root\\cimv2 ");
scope.Connect();

System.Management.ObjectQuery q = new System.Management.ObjectQuery ( "select SerialNumber from Win32_BIOS ");

ManagementObjectSearcher searcher = new ManagementObjectSearcher (scope, q);
ManagementObjectCollection collection = searcher.Get();

StringBuilder sb = new StringBuilder();
foreach (ManagementObject o in collection)
{
sb.AppendFormat( "DELL ServiceTag: {0}\r\n ",o.GetPropertyValue ( "SerialNumber "));
}
textBoxOutput.Text = sb.ToString();

}

------解决方案--------------------
string GetSerialNumber(string drive)
{
ManagementObject disk=new
ManagementObject( "win32_logicaldisk.deviceid=\ " " + drive + ":\ " ");
return
disk.Properties[ "VolumeSerialNumber "].Value.ToString());
}