private bool GetDiskSerial(ManagementScope scope, ArrayList hdCollection, ref ManagementObjectSearcher searcher) { try { ObjectQuery query1 = new ObjectQuery("SELECT * FROM Win32_PhysicalMedia"); searcher = new ManagementObjectSearcher(scope, query1); int i = 0; string sDiskSerial = ""; foreach (ManagementObject wmi_HD in searcher.Get()) { // get the hard drive from collection // using index if (i < hdCollection.Count) { HardDrive hd = (HardDrive)hdCollection[i]; if (wmi_HD["SerialNumber"] == null) { hd.SerialNo = ""; } else { hd.SerialNo = wmi_HD["SerialNumber"].ToString(); } } ++i; } foreach (HardDrive hd in hdCollection) { if (!String.IsNullOrEmpty(hd.SerialNo)) { sDiskSerial = hd.SerialNo; break; } } return(true); } catch (Exception) { return(false); } }
private bool GetDiskDrive(ManagementScope scope, out ArrayList hdCollection, out ManagementObjectSearcher searcher) { try { ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_DiskDrive"); hdCollection = new ArrayList(); searcher = new ManagementObjectSearcher(scope, query); foreach (ManagementObject wmi_HD in searcher.Get()) { HardDrive hd = new HardDrive(); hd.Model = wmi_HD["Model"].ToString(); hd.Type = wmi_HD["InterfaceType"].ToString(); hdCollection.Add(hd); return(true); } return(true); } catch (Exception) { hdCollection = null; searcher = null; return(false); } }